But where will we keep it? How about a class level variable. That way when we get down to the parsing the parsing function will have access to the file as well. Now we need the code to load the template file. It is very simple. Put this right after the comment in the constructor indicating that we have a file:.
We now need only to parse the data and display it back. Remember that our data is contained in an associative array. That array is going to be sent to our parsing method. Seems pretty simple right? Well it is! Last thing we need is to display it to the user. This will be one of the easiest methods you ever have to write. We are done. You now have a complete working template system.
Like I mentioned before this is a really simple system. Once you get better aquainted with template systems you will probably want to expand it. I would like to be able to simply pass my Class function a PHP Array where the Array key is the variable name and the value is the value that would be populated in my email template.
I am curious how I could best achieve this? My main question would be how to pass in the PHP Array and have it map out to a variable name to do the replacements. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 6 years, 10 months ago.
Active 2 years, 2 months ago. Viewed 6k times. JasonDavis JasonDavis Any template system like Twig combined with any mailing class like SwiftMailer will do the trick. I dont need a template library or mailler, just need to know how to make an array convert to the variable names for the replacements.
The whole Class should be 1 file and not even that large for what I am doing! Thanks though — JasonDavis. This is a simple function that takes a filename as an argument, checks if the file actually exists and returns the content. In our case the key from the array surrounded by hashtags.
Argument2 : The string to replace the found string with. For us it's the value corresponding to the key in the array. Argument3 : The whole string to search and replace in. This would be our template content.
We could create the array directly, like this: don't use this code though, we'll use a better example. Read on! For the sake of this tutorial, using a database as an example is probably too comprehensive, so let's do it with JSON. Create a file called content. To briefly explain, the value to the left, before the colon : is known as the key.
0コメント