Jump to content
LAPS

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

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.

Share this post


Link to post
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;

 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...