Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Re your code:</p> <pre><code>$keys = array( 'TITLE' =&gt; 'This is page title', 'HEADER' =&gt; 'This is some header', 'GALLERY' =&gt; 'while($row = mysql_fetch_array($result)){&lt;li&gt;&lt;img src="'.$row['filename'].'"/&gt;&lt;/li&gt;})' ); </code></pre> <p>I would consider it very poor practice to have PHP code embedded in your template data this way. Allow me to offer you a completely different solution...</p> <p>Firstly, we need to encapsulate the code. The code you've shown above is trying to fetch data from a <code>$result</code> variable, but <code>$result</code> itself is obviously been set elsewhere. This isn't good, because if we ever want to change the way the gallery feature works, we would need to search all over the code to find different bits of it.</p> <p>Instead, you should write the feature as a self-contained function (or a class if it's too complex for a single function), which would load the data, and process it.</p> <p>Let's call that function <code>loadGallery()</code>. It might look something like this:</p> <pre><code>function loadGallery() { $output = ''; $result = mysql_query('...gallery query here...'); while($row = mysql_fetch_array($result)) { $output .= "&lt;li&gt;&lt;img src='{$row['filename']}'/&gt;&lt;/li&gt;"; } return '&lt;ul&gt;'.$output.'&lt;/ul&gt;'; } </code></pre> <p>Obviously, it could be a lot more complex than that (eg you may want to paginate it, or offer category options; these would be written as parameters for the function). I've also not written any error checking, etc here for brevity.</p> <p>Now that you've got that function, you can plug it into the template engine. You need to reference the function in your template.</p> <p>You currently have template markers like this <code>{TITLE}</code>. In your code, these markers are swapped with plain text using <code>str_replace()</code>. That's fine. But in this case we now want to call a function instead, so we need to have a different kind of marker.</p> <p>Let's write the gallery marker like this: <code>{FUNCTION:loadGallery}</code> instead of <code>{GALLERY}</code> as you currently have.</p> <p>Now you can have an additional bit of code in your template engine that looks at these <code>{function:}</code> markers and replaces them with a function call. (You can keep the existing simple <code>str_replace()</code> method as well of course)</p> <pre><code>$funcReplaces = array( //similar to your existing $keys array, but for allowed functions 'loadGallery' ); foreach($funcReplaces as $replaceFunc){ $template = str_replace('{function:' . $replaceFunc . '}', $replaceFunc(), $template); } </code></pre> <p>This will run the function and put the output into the template.</p> <p>So that answers the question for you.</p> <p>What I should point out, however, is that there are a <em>lot</em> of other issues that you need to think about when writing a templating engine, from both a technical and security perspective. The above describes a basic way to resolve the specific question at hand, but it <em>isn't</em> a fantastic all-singing all-dancing template engine. It's still just a pretty basic one.</p> <p>That's fine, if that's all you need, but if you expect this system to grow, it's worth noting that this whole area is that this is very much a problem that others have already solved, and solved well. There are several very good templating engines available for PHP. I would suggest that your best course of action would be to investigate some of them, and maybe use one of those instead of writing your own.</p> <p>Some that you could try:</p> <ul> <li><a href="http://www.smarty.net/" rel="nofollow">Smarty</a></li> <li><a href="http://twig.sensiolabs.org/" rel="nofollow">Twig</a></li> <li><a href="https://github.com/bobthecow/mustache.php" rel="nofollow">Mustache</a></li> </ul> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload