Get current page title from API

Author Message

Bastien Bobe

Wednesday 28 May 2008 3:33:09 am

Hi,

I'm a developper of an open source web analytics software (spongestats.org).

I have developped a new feature for version 3.1 to get the page title from the current page based on an API.
Actually, only Wordpress is integrated but I would like to integrate some other CMS/Blog platforms, so I would like to know if there is an API feature included in MODX that I can use to get the current page title.

For example, in Wordpress, I use the API feature wp_title('',false);

Do you know if something similar is available in the EZ API and if I can use it in an external marker ?

Regards,
Bastien.

Piotrek Karaś

Wednesday 28 May 2008 8:39:38 pm

Bastien,

What exactly do you mean by page title? Contents of the title tag, or maybe the name of the node? What are your restrictions on what a title might be?

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Bastien Bobe

Thursday 29 May 2008 2:04:11 am

The title of the HTML page will be fine or at least the content of the main article display in the page.

I don't really know the ezpublish behaviour but I think this is like SPIP so if I can get the content of the text displayed in the <title> markup it will be fine.

Do you know if I can do that ?

Piotrek Karaś

Thursday 29 May 2008 2:48:44 am

If you aim at getting the title in a universal way, that it would be difficult in my opinion. eZ Publish makes it possible to edit the XHTML head templates and that is by no means hacking, but standard customization. For example, we change the default title-generation rules in most of our integrations, and those also follow different algorithms. Also, for a lot of people that would take place in the template itself, so there will be no title-dedicated variable in the system, that you could easily reach for.

Now, one thing that you could try, which is also available at API level, are the object names, which are names based on selected attribute values of content objects. In most cases, they are kept short. Unfortunately, they do not reflect the structure by themselves.

Now, I have no idea how that information is relevant to your needs - hope it could help a bit.

Cheers,
Piotrek

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Bastien Bobe

Thursday 29 May 2008 2:55:23 am

OK, so by default, what is the PHP code included in the defaut template between the HTML <title></title> markups ?

Pascal Specht

Thursday 29 May 2008 4:07:50 am

Hi Bastien,

if this is of help for you, here is the code I have at the end of my pagelayout template to add a XiTi tracker. The XiTi tracker is similar to Google analytics etc, and I needed to have the page title as a variable, to use it inside the javascript code:

(... other code, inside this pagelayout.tpl)

<!-- Xiti starts here -->

{let name=Path
     path=$module_result.path
     reverse_path=array()}
  {section show=is_set($module_result.title_path)}
    {set path=$module_result.title_path}
  {/section}
  {section loop=$:path}
    {set reverse_path=$:reverse_path|array_prepend($:item)}
  {/section}

{set-block scope=root variable=site_title}
{section loop=$Path:reverse_path}{$:item.text|wash}{delimiter} / {/delimiter}{/section} - {$site.title|wash}
{/set-block}
{/let}

{literal}
<a href="http://www.xiti.com/xiti.asp?s=999999" title="WebAnalytics">
<script type="text/javascript">
<!--
{/literal}
Xt_param = 's=999999&p={$site_title|wash(javascript)}';
{literal}
try {Xt_r = top.document.referrer;}
catch(e) {Xt_r = document.referrer; }
Xt_h = new Date();
Xt_i = '<img width="80" height="15" border="0" alt="" ';
Xt_i += 'src="http://logv3.xiti.com/g.xiti?'+Xt_param;
Xt_i += '&hl='+Xt_h.getHours()+'x'+Xt_h.getMinutes()+'x'+Xt_h.getSeconds();
if(parseFloat(navigator.appVersion)>=4)
{Xt_s=screen;Xt_i+='&r='+Xt_s.width+'x'+Xt_s.height+'x'+Xt_s.pixelDepth+'x'+Xt_s.colorDepth;}
document.write(Xt_i+'&ref='+Xt_r.replace(/[<>"]/g, '').replace(/&/g, '$')+'" title="Internet Audience">');
//-->
</script>
<noscript>
{/literal}
Mesure d'audience ROI statistique webanalytics par <img width="80" height="15" src="http://logv3.xiti.com/g.xiti?s=999999&p={$site_title|wash(javascript)}" alt="WebAnalytics" />
</noscript></a>

<!-- Xiti ends here -->

as you can see, the variable $site_title will contain the page title. That way, I transmit the current page title to the JS code.

I hope you can reuse some bits of this in your development,

Best regards,
</Pascal>

Bastien Bobe

Thursday 29 May 2008 4:43:36 am

Thanks for the info Pascal but my script is in PHP (sorry, I did not say that) and with this server language this is not possible to get the current page title as we can do in JS with a simple document.title.

If I had the content of the defaut HTML template, I think it could help me, does someone can paste me the <title></title> markups of the source default template ?

Pascal Specht

Thursday 29 May 2008 5:04:45 am

Hi Bastien,

well, the standard code for that is:

    <title>{$site_title}</title>

which can be found in the template called design\standard\templates\page_head.tpl.

The way eZ Publish works is that every request will first get to the module handler, which will process the template variables like $site_title, and which after that will produce the XHTML code.

Does this help? There's no such a function called get_page_title( ) or so.

If you want to further discuss this outside of this forum, you may contact me at ezpublish (at) bizandgo (dot) com.

</Pascal>

Felix Laate

Thursday 29 May 2008 6:24:32 am

Hi there,

in PHP you can get the site title like this:

$site_title = $ini->variable( 'SiteSettings', 'SiteName' );

This gives you the title as specified in <i>site.ini</i>. The title as shown on any ezp-page however, consists of the reversed path + this title.

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

Bastien Bobe

Thursday 29 May 2008 7:09:40 am

OK, so could you try to add this code to your ezpublish template to execute it on each page and see what value does it return for each page :

function get_page_title()
{
$current_title = $GLOBALS['ini']->variable( 'SiteSettings', 'SiteName' );
echo "Page title : $current_title";
}
get_page_title();

I can't test it on a ezpublish platform but if this script really show you the page of the title, I can provide you the BETA of SpongeStats 3.1 to see if there is something wrong.

Thank you very much for your help.
Regards,
Bastien.

Pascal Specht

Thursday 29 May 2008 7:37:42 am

Sorry Bastien, but you can not execute PHP from a eZ Publish template like this.

Variables in the eZ Publish template language also use a trailing dollar sign, but that is not PHP code; including PHP code as you suggest won't get you any results.

Since you probably are not familiar with eZ Publish, I can point you to a way you can execute PHP code inside a template, but it's a bit complicated:

You would have to write a so called template operator ainside a custom extension. This template operator would then execute your PHP code, and return the title. The code would then be called inside the template. Direct calls to PHP can't be done.

The system in eZ Publish already fills in the title section by using an intelligent name (that for example contains the forum name, the site name etc). This is then put into the eZ Publish variable. And in the final step, the template renders the content of that variable.
Putting anything else in the <title> section of the template would not produce anything. So even if you could write this template function, it would not be work. It can't sense the page title, since the page is not yet rendered in the template at that very moment.

I would suggest you setup a eZ Publish install somewhere to understand the template system before going any further. Setting up a one-shot eZ Publish 3.10 is a matter of 1/2 hour. (Understanding the template language although is a matter of 1/2 a month!)

Regards,
</Pascal>

Bastien Bobe

Thursday 29 May 2008 7:55:53 am

OK, so if it's not possible to include a PHP marker in the template, you can't use an external PHP tool to manage your webstats ?

If this is the case, I don't have to spend some time on the ezpublish get_title function :)

André R.

Thursday 29 May 2008 8:07:50 am

You need to create a template operator to call php code from template language.
It's quite simple, and here is a simple example extensions for eZ Publish:

http://ez.no/developer/contribs/template_plugins/httpinclude

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.