Jump to content

Foreach loop commented out in HTML page


mowerol
 Share

Recommended Posts

Hi everyone,

I'm new to processwire and have just started playing around with it for the past week. At the moment, I'm trying to use repeater fields and am having difficulty getting them to output to my page. Instead, what bizarrely seems to be happening is that half of the foreach loop declaration seems to be appearing as plain text on my page and is commented out when I check inspect element.

 

I'm not sure if my syntax is wrong or this is an entirely different problem. Any help would be hugely appreciated!

Here's my code:

<?php $portfolio=$pages->get("name=portfolio");?>

<? foreach ($portfolio->portfoliopreviews as $portfoliopreview): ?>
<a href="#">
	<div class="port">
		<img class="pic" src="<?=$portfoliopreview->portimg->url;?>"/>
		<div class="portbgrdtext">
			<h5><?=$portfoliopreview->portgenre;?></h5>
			<p><?=$portfoliopreview->porttext;?></p>
			<button class="portbtn">Read More</button>
		</div>
	</div>
</a>
<? endforeach; ?>

And in the attached images you can see what's happening on the page and within inspect element.

You can see that the div is being created, albeit without any fields populating inside of it. You can also see that loop is being commented out within inspect element.

Cheers!

 

screenshot-127.0.0.1 2016-07-05 12-02-41.png

inspect.png

Link to comment
Share on other sites

The php server environment where you use this code, has short php syntax disabled.

To be save and transportable with your code, you never should use this. Always use <?php to enter into php mode. Better don't use <?

Link to comment
Share on other sites

Thanks, but I've used short syntax throughout my file and have had no problems except with this one.

When I make it <?php for some reason I now get this error:

Quote

Parse Error: syntax error, unexpected end of file (line 89 of C:\xampp\apps\processwire\htdocs\site\templates\home.php) 

There isn't any code on line 89 so it's referring to the end of  my doc. Would this mean something else is wrong?

 

//Edit - Cheers again horst. I just changed the endforeach to use <?php too and that's resolved the problem!

Edited by mowerol
Update
Link to comment
Share on other sites

This sounds like you're missing a <?php or a ?> somewhere in that file either or some other syntax error, which puzzles the parser. Essentially the parser is mixing up php and html content different to what you intended.

  • Like 1
Link to comment
Share on other sites

I think that you:

a) have only used short syntax for echo(ing) strings: <?= what is not the same as the short php opener. Please check and confirm. :)

b) now, it is parsed and tells you that you have a parse error. A parse error at the last line of a file occures when you somewhere have an unclosed level of, e.g. foreach, if statement or something else.

You have to go through your code and check if you have closed each opened one.

In tis regard, I want to say that the code you use is not the best solution. I personally find it really hard to read, and it also is hard to maintain.

There are many ways to keep the logic aways from the markup. Big and small solutions. But everything seems to be better than that. For example, you can do the php logic in the top of the file, and than merge it together with the markup.

<?php // open the file in PHP mode, 
/** add comments to the file if needed. 
  * This is a good place here. :)
  **/

// define a markup template with placeholders
$myMarkupTemplate = "
<a href='#'>
    <div class='port'>
        <img class='pic' src='[_URL_]'/>
        <div class='portbgrdtext'>
            <h5>[__GENRE__]</h5>
            <p>[__TEXT__]</p>
            <button class='portbtn'>Read More</button>
        </div>
    </div>
</a>
";

// now get your page and loop through your selections
$portfolio = $pages->get("name=portfolio");
foreach($portfolio->portfoliopreviews as $portfoliopreview) {
    $search = array('[_URL_]', '[__GENRE__]', '[__TEXT__]'); 
    $replace = array($portfoliopreview->portimg->url, $portfoliopreview->portgenre, $portfoliopreview->porttext);
    echo str_replace($search, $replace, $myMarkupTemplate);
}

Another approach could be

<?php
// get your page and loop through your selections
$portfolio = $pages->get("name=portfolio");
foreach($portfolio->portfoliopreviews as $portfoliopreview) {
    $url = $portfoliopreview->portimg->url;
    $genre = $portfoliopreview->portgenre;
    $text = $portfoliopreview->porttext;
    echo "
      <a href='#'>
          <div class='port'>
              <img class='pic' src='{$url}'/>
              <div class='portbgrdtext'>
                  <h5>{$genre}</h5>
                  <p>{$text}</p>
                  <button class='portbtn'>Read More</button>
              </div>
          </div>
      </a>\n";
}

or you ommit the temporary variables and put out the field values directly:

<?php
// get your page and loop through your selections
$portfolio = $pages->get("name=portfolio");
foreach($portfolio->portfoliopreviews as $portfoliopreview) {
    echo "
      <a href='#'>
          <div class='port'>
              <img class='pic' src='{$portfoliopreview->portimg->url}'/>
              <div class='portbgrdtext'>
                  <h5>{$portfoliopreview->portgenre}</h5>
                  <p>{$portfoliopreview->porttext}</p>
                  <button class='portbtn'>Read More</button>
              </div>
          </div>
      </a>\n";
}

There are many ways one can go. :)

 

  • Like 3
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

×
×
  • Create New...