Search code examples
templatesmeta-tagscontao

Checking conditions in fe_page template for Contao


I am now working with contao fe_page template for generating meta tags. Now i need to display a meta tag based on a condition. That is

    if (condition a){
        meta tag =a
}
else {
        meta tag = b
}

The condition is "whether the news module is used in that page or not". How can I check for dat condition in fe_page template? Is there any solution for this?


Solution

  • In stead of checking it in your fe_page template, you can do the following in your news_full template:

    <?php
    
    if ($this->addImage)
    {
        $GLOBALS['TL_HEAD'][] = '<meta name="twitter:card" content="summary_large_image">';
        $GLOBALS['TL_HEAD'][] = '<meta name="twitter:image" content="' . $this->singleSRC . '">';
    }
    

    As you can see this also adds a twitter:image meta attribute with the news article's teaser image.

    Instead of doing it in a news_full template, you could also do it in a parseArticle hook, which gives you a little more control, in case you need it.