Hi @twa,
There are two pieces to this:
- Finding out what PHP code is responsible for calling the template, and
- Determining how best to intervene in that PHP code to add your new functionality.
For the first challenge, we need to search the code for references to the article_details.tpl
template. You can use a search tool for this – I use grep
:
$ fgrep -l article_details.tpl `find . -name \*.inc.php` `find . -name \*.tpl`
./templates/frontend/objects/article_details.tpl
./templates/frontend/pages/article.tpl
So ignoring the fact that the template mentions itself, it’s referred to from articles.tpl
. That’s still a template, and we’re looking for PHP, so we’ll search one step back, and use the same trick to find out what calls articles.tpl
:
$ fgrep -l article.tpl `find . -name \*.inc.php` `find . -name \*.tpl`
./pages/article/ArticleHandler.inc.php
./templates/frontend/pages/article.tpl
./templates/frontend/components/breadcrumbs_article.tpl
Once again the article.tpl
template refers to itself (and we have a false hit on breadcrumbs_article.tpl
, since it ends with article.tpl
as well).
The relevant code is pages/article/ArticleHandler.inc.php
. This is what sets up all the variables your Smarty templates will require. Looking for article.tpl
in that code, you’ll see the following in the view
function:
return $templateMgr->display('frontend/pages/article.tpl');
OK, good, so you could go ahead and simply modify that code to add more variables to one of the $templateMgr->assign
calls to send more data to the template file.
However, you mention that you’re working with a child theme. Is that where you’re modifying article_details.tpl
? If so, it’s better to keep your changes in that theme as well. This will be a little more complicated, but will build on what I’ve already described.
For an example of this, look at the “Critical Times” plugin. It registers a hook to watch out for template loads:
HookRegistry::register ('TemplateManager::display', array($this, 'loadTemplateData'));
…and then in the loadTemplateData
function, it watches for article.tpl
to be referred to…
if ($template === 'frontend/pages/article.tpl') {
$this->loadArticleTemplateData($hookName, $args);
}
…and that function in turn is free to add whatever data it likes to the template before it’s displayed.
To solve your specific problem, because I think you’re probably looping through all the authors, it would be best to load the full set of countries and assign that to the template manager:
$countryDao = DAORegistry::getDAO('CountryDAO');
$templateMgr->assign('countries', $countryDao->getAllCountries());
…then in the template, look it up from the array:
{assign var=authorCountryCode value=$author->getCountry()}
Country: {$countries.$authorCountryCode|escape}
Consider this all pseudocode – I haven’t tested it.
Regards,
Alec Smecher
Public Knowledge Project Team