Jump to content

Template (php)


mel47
 Share

Recommended Posts

Hi,

So, first question...

To learn how to use PW, I tried to build a really basic news system. So I created a news template with 4 fields - title, date, summary, image. Done.

After, as I was not sure I started from basic-page.php and tried to modify by using HTML from my old website. After many trials and errors, I succeed to display something.

<?php namespace ProcessWire; 

$content = "<article><h2>" . $page->body  . "</h2>
<header class='news-meta'>
<strong>Date : </strong> " . $page->get("publish_date") . "
</header>
<div class='news-content-text'>
<p>". $page->get("summary") . "</p>
</div>
<div class='article'> text </div>
" . "<p><img src='" . $image->url."' alt='". $image->description . "' ></p> </article>"; //image doesn't work!!

// If the page has children, then render navigation to them under the body.
// See the _func.php for the renderNav example function.
if($page->hasChildren) {
	$content .= renderNav($page->children);
}

// if the rootParent (section) page has more than 1 child, then render 
// section navigation in the sidebar (see _func.php for renderNavTree).
if($page->rootParent->hasChildren > 1) {
	$sidebar = renderNavTree($page->rootParent, 3); 
	// make any sidebar text appear after navigation
	$sidebar .= $page->sidebar; 
}

 

So my questions :

  1. I don't like it, way too much concatenation and I'm lost between " and '. How I should do that?
  2. Why this template is not similar to main.php with a part in php and one in html? In fact, my problem is that I'm not sure how to mix them
  3. Why page->body doesn't throw all fields of the template? More specifically, what is it? I can't find it in the cheatsheet...
  4. Why basic-template.php have an open <? php but any closing bracket?

Thanks!!

Mélanie

Link to comment
Share on other sites

1.)
 

"<article>
    <h2>{$page->title}</h2>
    <header class='news-meta'><strong>Date : </strong>{$page->publish_date}</header>
    <div class='news-content-text'>
        <p>{$page->summary}</p>
    </div>

    <div class='article'>{$page->body}</div>
    <p><img src='{$image->url}' alt='{$image->description}'></p>
</article>";

 

_main.php should be the global layout; the templates just populate the content variable which is within the content area of the '_main' layout.

$page->body is the "body" field (the one with the Rich Text Editor)

Closing brackets are not needed and should always be left out when the file ends with php code: https://processwire.com/api/coding-style-guide/#2-general

 

 

 

  • Like 2
Link to comment
Share on other sites

1 hour ago, mel47 said:

"<p><img src='". $image->url."' alt='". $image->description . "' ></p> </article>"; //image doesn't work!

If your image field is named 'image' use the following api:

// single image field
$page->image->url
$page->image->description

//multi image field
$page->image->first()->url
$page->image->last()->url
$page->image->eq(5)->url

// your example
$content = "<p><img src='{$page->image->url}' alt='{$page->image->description}' ></p>";


1 ARROW: use curled brackets OR paste as is between double quotes
2 ARROWS and more: you need to use curled brackets, as I did in the example with the image

$content = "<article><h2>$page->body</h2>";


Please read some tutorials and informations in the docs, before going further

https://processwire.com/api/templates/

https://processwire.com/docs/tutorials/default-site-profile/
https://processwire.com/docs/tutorials/simple-website-tutorials/

 

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