Jump to content

Set a HTML code block in advance to output it later in the template file


LAPS
 Share

Recommended Posts

Hi there,

I would like to set a HTML code block in advance to output it later in the template file.

I found I can make that this way:

<?php $html_code = function() { ?>
  ... some HTML here...
<?php }; ?>

<?= $html_code(); ?>

However it doesn't sound to me the goodest way of doing things.
Furthermore it seems there isn't a smooth way to pass PW variables within the block.

Is there a PW API method to make that a better way?

 

UPDATE
I just found this.

Link to comment
Share on other sites

It depends on the specific case, some examples

$tpl = file_get_contents($config->paths->templates . 'html/my-file.html'); //ex: <p>This is a test: {foo}, and this is another test: {bar}</p> ..or..
$tpl2 = "<p>This is a test: {foo}, and this is another test: {bar}</p>";

$vars = [ 
    'foo' => 'FOO!',
    'bar' => $pages->get('/')->title,
];

// Sometime later
echo wirePopulateStringTags($tpl2, $vars);

..or you can use PHP heredoc

$testing123 = "123";

$html = <<<HTML
  <div class='something'>
    <ul class='mylist'>
      <li>FOO!</li>
      <li>{$pages->get('/')->title}</li>
      <li>$testing123</li>
    </ul>
  </div>
HTML;

// Sometime later
echo $html;

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...