Jump to content

Newbie question - Very basic output


Mark
 Share

Recommended Posts

Hi, I am going through the tutorials found on:

http://processwire.com/api/templates/ 

I would like to output my body field but the code found on the templates info page is not working for me!

<div id='body'>
    <?=$page->body?>
</div>
 

This however is! The code below was found in the templates folder (frontpage.php)

echo "<p>" . $page->body . "</p>";
 

Are the two snippets of code supposed to output the body to the page in the same way (ignore the styling).

I'm sorry for the silly question......I'm new! :)

Link to comment
Share on other sites

Greetings Mark,

Welcome to ProcessWire!

That looks like it should work, but a couple of things that sometimes don't work:

1. Short tags.  Instead of this:

<?=$page->body?>

Try this:

<?php echo $page->body; ?>

2. Double quotes for referencing the body ID, like this:

<div id="body">

These are not really a ProcessWire issues, but general HTML things.

Try it and let us know!

Thanks,

Matthew

Link to comment
Share on other sites

It's probably that your php settings don't include short_open_tag, so

<?=$page->body?>

won't work.

<?php echo $page->body; ?>

is exactly equivalent, and if (as is the default) your body field is coming from a tinymce input, it will already include <p> and </p>, so you don't really need to output them again.

<edit>Damn, Matthew beat me to it.</edit>

Edited by DaveP
Link to comment
Share on other sites

Hi Matthew and Dave,

Thanks very much for your fast reply....Matthew was quicker by 3 minutes :)

I have attached the full php code to hopefully show what I'm doing wrong. 

<?php 

include("./TUT_header.inc"); 

echo "<h2>" . $page->title . "</h2>"; // Working!
<?php echo $page->body; ?> // Not working!



include("./TUT_footer.inc");
 

without using tinymce what would be the best way to style the static 'Direction' red and the dynamic 'direction' blue using your method? Thanks, Mark

echo "<p>Direction: " . $page->direction . "</p>"; // Working!
 
Link to comment
Share on other sites

Mark, I think the source of your confusion here is that you still don't understand how PHP works. Unlike other programming languages, PHP can integrate with HTML in two ways: by integrating it, or by being integrated inside it (ok, strange sentence, but I hope it delivers the point).

these two do the same thing:

<?php echo "<h2>" . $page->title . "</h2>"; ?>

<h2><?php echo $page->title; ?><h2>

The difference is that, in the first code you are telling PHP to output all the content, while in the second example, you are using it to output only the dynamic content between static content. The important thing to understand (and this was your mistake), is that you start the file outside PHP, and then you can enter it with "<?php" and leave it with "?>". You can never open a PHP tag, when you are already inside another, like here:

<?php <-- we opened PHP
include("./TUT_header.inc");

echo "<h2>" . $page->title . "</h2>";
<?php echo $page->body; ?> <-- and we opened it again without having closed it before

You should read an introduction to PHP to learn all these concepts, and you will see that PW will be much easier then.

  • Like 4
Link to comment
Share on other sites

I'm amazed by the PW community. Thank you so much everyone, I really appreciate your help.

Diogo, yes, it's the PHP that was confusing me. I will have to get back to basics and understand the rules of PHP. Thanks for your clear explanation. 

Totoff, thanks for help with the code plus adding the style. 

  • Like 1
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...