Jump to content
alanxptm

How to find pages by template filename?

Recommended Posts

Hi

I want to list the latest articles on the site, the articles have multiple templates using the same template php archive.

I have tried with:

$pages->find('template=lvl04-post, limit=5');

but didn't work. Also, I have read about $template->filename, but don't understando how to use it with find()

Articles and parent categories have their own single templates because of role permissions, so I can't just do a normal 'template=name'

This is the full code:

<?php $posts = $pages->find('template=phpfilename?, limit=5');
foreach($posts as $post){ ?>
<a href="<?php echo $post->url; ?>"><?php echo $post->title; ?></a><br />
<?php echo date("d/m/Y", $post->published); ?><br/>
<?php echo $post->resume; ?><br/>
<?php } ?>

Any help is appreciated 😮

Share this post


Link to post
Share on other sites

That code seems correct.  Are any of the post pages in-fact published, because if not, you'll need to add 'include=all' to your selector:

$pages->find('template=lvl04-post, limit=5, include=all');

 

  • Like 1

Share this post


Link to post
Share on other sites

I mean "lvl04-post.php" pages have other Processwire templates, like: cat01-post, cat02-post, cat03-post, etc. 

Share this post


Link to post
Share on other sites

Try:

// Set filename manually
$filename = $config->paths->templates . 'lvl04-post.php';
// Or get filename from one of the templates that uses it
// $filename = $templates->get('lvl04-post')->filename;

// Find templates that use this template file
$tpls = $templates->find("filename=$filename");

// Find pages using those templates
$posts = $pages->find("template=$tpls, limit=5");

 

  • Like 5

Share this post


Link to post
Share on other sites

Sadly it didn't work, but I've already changed all the site and template structure so I can find by the single template name. I suppose is the best way, but would like to have the option to find by archive name, as it is an option in the template settings 😮

Share this post


Link to post
Share on other sites
22 minutes ago, alanxptm said:

Sadly it didn't work

It works for me, so double-check your code.

Another way you can find related templates is to apply a tag to them in the admin (Advanced tab). Then you can find the templates that have that tag and find pages using those templates.

// Find templates with tag "foo"
$tpls = $templates->find("tags=foo");

// Find pages using those templates
$items = $pages->find("template=$tpls");

 

  • Like 2

Share this post


Link to post
Share on other sites

I just needed this again and this time I've found a simple solution..

I was so focused on PW functions instead of plain PHP 😅

$tmplt = basename($page->template->filename,'.php');

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By pwFoo
      Hi,
      I use TemplateFile class to render custom files like _layout.tpl (base html layout file), but output I think output isn't cached. Because there is no template added to PW I can't configure template cache I think.
      How to activate template cache inside of php code temporarily or persistent (for example with a hidden / system template created)? 
      Should be done with code (module install or inside a php file).
    • By pwFoo
      Module: http://modules.processwire.com/modules/template-file-helper/
      Repo: https://bitbucket.org/pwFoo/templatefilehelper/overview
       
      TemplateFileHelper module features
      add global controller and template to current page by a Page::render hook Manage global ($layout) and current page ($view) styles and scripts with a $config->scripts / $config->styles context mapping. So $config->styles / $config->scripts works fine too load sub-templates with a controller file  an array of data to fill template variables just an html template Ajax page load in mind  Usage
      Global layout
      A global controller / template is added by a Page::render hook.
      /site/templates/_layout.php  // controller  /site/templates/_layout.tpl  // view / html template  
      Example _layout.tpl
      <!doctype html> <html lang="de">   <head>     <meta charset="utf-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <title>TemplateFileHelper Processwire</title>     <?=$styles?>     <?=$scripts?>   </head>   <body>       <div id="nav"><?=$navigation?></div>       <div id="pageContent"><?=$pageContent?></div>   </body> </html>
      Example _layout.php
      // MarkupSimpleNavigation $nav = $modules->get('MarkupSimpleNavigation'); $layout->set('navigation', $nav->render($opts)); // Global and current page styles $styles = ''; foreach ($layout->styles as $style) {     $styles .= "<link href='{$style}' rel='stylesheet' class='global'>"; } foreach ($view->styles as $style) {     $styles .= "<link href='{$style}' rel='stylesheet' class='current'>"; } $layout->set('styles', $styles); // Global and current page scripts $scripts = ''; foreach ($layout->scripts as $script) {     $scripts .= "<script src='{$script}' type='text/javascript' class='global'></script>"; } foreach ($view->scripts as $script) {     $scripts .= "<script src='{$script}' type='text/javascript' class='current'></script>"; } $layout->set('scripts', $scripts);  
      Current page
      The PW template of current page will rendered inside the global view by Page::render hook.
      /site/templates/basic-page.php  // controller  /site/templates/basic-page.tpl  // view / html template  
      Example basic-page.tpl
      <div><?=$contentHome?></div>
      Example basic-page.php
      $view->set('contentHome', 'Simple output...'); echo $view->render();
      Output (for example to debug) is possible too.
      echo "My PW template file output..."; $view->set('contentHome', 'Simple output...'); echo $view->render();  
      Sub-templates
      It's possible to use sub-templates / chunks inside of a PW template / controller.
      Sub-template with controller / view files
      $part = $view->load('parts/test1', 'parts/test1');  // relative to /site/templates (view = .tpl, controller = .php $part = $view->load('parts/test1', true);   // same as above. "True" instead of write identical path / file twice  
      $part = $view->load('parts/test1', 'parts/test1_controller');  // view "parts/test1.tpl", controller "parts/test1_controller.php"
      Sub-template with array data
      $part = $view->load('chunks/test1', array('variable1' => "value1", 'variable2' => 'value2'));  
      Sub-template just a html chunk
      $part = $view->load('chunks/test1');   // view file /site/templates/chunks/test1.tpl  
       
      PW template file as view
      Because direct output inside a PW template file is possible it also works without a view.
      Example: PW template without viewTested with the FrontendUser module quick and dirty...
       
      $fu = $modules->get('FrontendUser'); $fu->login(); $button = $fu->form->fhSubmitBtn; if (!empty($_GET['logout'])) {     $fu->logout($page->url); } $processed = $fu->process($page->url); if ($processed && !$user->isGuest()) { // $processed == false if login failed (not submitted / login successful == true)     echo "Hello $user->name!";     echo "<a href='$page->url?logout=1'>Logout</a>"; } else {     echo $fu->render(); }  

      Scripts / Styles context
      The module itself takes care about the global (inside _layout.php) and "current" (inside PW template file). Just use PW $config to set styles and scripts.
      $config->scripts->add('...'); $config->styles->add('...');  
      You can also force the context by use the additional global api variables.
      $layout->scripts->add('...');  // global context $layout->styles->add('...');   // global context  
      $view->scripts->add('...');  // current page context $view->styles->add('...');   // current page context  
    • By lenoir
      Hi there,
      Simple question needs simple answer… I'm still a newby and trying to get my head round pw.
      I'm trying to access (and eventually get the path) to a particular page somewhere else in the tree, without knowing the complete path, only knowing the name of the page.
      I'm getting the name of the page with urlSegment. Code looks like this so far:
      $myPage=$input->urlSegment1; $pathToPage=$pages->find($myPage)->path; Played around with it, browsed the forum and failed embarassingly.
      Any suggestions?
      Thanks.
×
×
  • Create New...