Results 1 to 4 of 4

Thread: Check for condition in page template

  1. #1
    New user
    Join Date
    08-22-17.
    Posts
    15

    Default Check for condition in page template

    I am now using fe_page template for my page. I need to check whether news module is used in that page or not. How can we check for that condition in page template? And in that condition I also need to check whether image exists for that news or not? How these conditions can be implemented in page template for meta tags?

    I tried with html parsing for checking the div classes with mod_newslist. But it's not working. Is there any way to check this?
    Last edited by georgy; 10/20/2017 at 10:39.

  2. #2
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    485

    Default

    What do you like to accomplish. As I think fe_page template is the wrong place for that. Only change fe_page if it's necessary.

    In every template you can check page credentials like so
    PHP Code:
    <?php
    global $objPage;
    echo 
    $objPage->alias;
    echo 
    $objPage->(fieldname of table tl_page);
    ?>
    What do you mean with "meta tags"?

    You can try to modify the page TITLE tag by
    PHP Code:
    <?php
    global $objPage;
    $objPage->title 'my title';
    ?>
    You can use a template to put HTML code into the HEAD section.
    PHP Code:
    <?php
    $GLOBALS
    ['TL_HEAD'][] = '<meta name="property" content="value">';

    // analyze $GLOBALS for other places like HEAD
    ?>
    You can use such a template with a content element (CE) type HTML, a frontend (FE) module type HTML, a j_- or moo_-template (e.g. j_my-php-code) which you pick in page layout or by using the inserttag {{file::my-php-code.html5|php|inc|foo}}. This file has to be in folder templates/. Or use this code directly in your news template.

    In templates you can output the template vars with
    PHP Code:
    <?php
    $this
    ->showTemplateVars();
    ?>
    In news templates you can analyze the news with a NewsModel
    PHP Code:
    <?php
    print_r
    (\NewsModel::findByPk($this->id)); // Pk = primary key

    // or
    $myNewsModel = \NewsModel::findByPk($this->id);
    echo 
    $myNewsModel->headline;
    ?>
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

  3. #3
    New user
    Join Date
    08-22-17.
    Posts
    15

    Default Check for condition in page template

    Thanks for your reply. I will try to explain my case in detail.
    I need to display meta tags in every page's head section. And I am using fe_page template in all pages. So I extended this page template and used this in all pages, so that I need to change the template only here, to get it reflected in all pages. Is this the correct method?
    Secondly, I need to check a condition. That is,
    1) If news module is used in that page and if it contains an image, then write the meta tag as "<meta name="twitter:card" content="summary_large_image">"
    2) If news module is not used in that page, Then write the condition as <meta name="twitter:card" content="summary">
    My question is, whether it is possible to check the conditions in fe_page template?
    I tried with parseArticle hook, but the else condition doesn't work. Is there any method to check this condition?

  4. #4
    User Andreas's Avatar
    Join Date
    07-11-09.
    Location
    Mönchengladbach
    Posts
    485

    Default

    This is hardly possible and thus not best practice.

    Better:

    Create a template "j_extended-head.html5" and include it in your page layout. Content:
    PHP Code:
    <?php
    $GLOBALS
    ['TL_HEAD'][] = '<meta name="foo" content="bar">'
    $GLOBALS['TL_HEAD'][] = '<meta name="twitter:card" content="summary">';
    I take it that your're talking bout the news reader module. So now put this into your reader template.
    PHP Code:
    <?php

    if($this->addImage// addImage untested
    {
      foreach(
    $GLOBALS['TL_HEAD'] as $key => $value)
      {
        if(
    strpos($value'"twitter:card"'))
        {
          unset(
    $GLOBALS['TL_HEAD'][$key]);
        }
        break;
      }

      
    $GLOBALS['TL_HEAD'][] = '<meta name="twitter:card" content="summary_large_image">';
    }

    ?>
    Web-Development, Freelancer, Burgtech, XHTML, HTML5, CSS, PHP, Javascript, MooTools, MySQL and more
    Amazon wishlist

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •