Jump to content

Recommended Posts

Posted

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! :)

Posted

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

Posted (edited)

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
Posted

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!
 
Posted
echo $page->body; 
 

Everything is starting to become a little clearer. It looks like when I try and make a reference to a .inc file it's throwing everything off. 

Posted

Chris on this way you advise php to Output a string which will be $page->Body in HTML and not the referenced data

Forget what i Said .... " and '

Shame on me

Posted

echo "<p class='alert'>Direction: <span class='noalert'>" . $page->direction . "</span></p>"; // Working!


.alert {color: red;}

.noalert {color:blue;}

  • Like 1
Posted

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
Posted

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
Posted

Stick at it Mark, a lot of us here were complete PHP beginners when we started. It won't take more than a couple of online video tutorials or reading some chapters in a book to get the hang of the basics :)

Posted

Hi onjegolders, Thanks for the encouragement. At least I can see instant mistakes with PW. Drupal made me clear the cache before I was allowed to see them! :)

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...