3

I am trying to pass the template name in my tag and wanted to do something like this:

<html id="template-{template_name}">

Any idea how to get the name of the template? Thinking aloud it'd be cool to also maybe suffix the template group as well. So a template embeds/_foo would return "embeds-_foo" ideally?

1
  • 1
    It depends a fair bit on how you've built your site and whether the template name is part of the URL structure so that you can grab it on the basis of segment_#. That may be kind of a late-parsed item though if you're looking to have it play a role in a conditional or in javascript targeting. Commented Jan 23, 2013 at 21:13

4 Answers 4

5

Are you looking for something as straightforward as this?

<html id="template-{segment_1}-{if segment_2 != ''}{segment_2}{if:else}index{/if}">

Ah, I see you are wanting embeds as well. Here is a very simple plugin that will output some metadata for the template that is currently being processed.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
Copyright (C) 2014 EllisLab, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
ELLISLAB, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of EllisLab, Inc. shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from EllisLab, Inc.
*/

$plugin_info = array(
                        'pi_name'           => 'Template Info',
                        'pi_version'        => '1.0',
                        'pi_author'         => 'Derek Jones',
                        'pi_author_url'     => 'http://ellislab.com/',
                        'pi_description'    => 'Outputs info for the currently processed template',
                        'pi_usage'          => Template_info::usage()
                    );


/**
 * Template_info Class
 *
 * @package         ExpressionEngine
 * @category        Plugin
 * @author          EllisLab Dev Team
 * @copyright       Copyright (c) 2014, EllisLab, Inc.
 * @link            http://ellislab.com/
 */

class Template_info {

    public $return_data;

    /**
     * Constructor
     *
     */
    public function __construct()
    {
        $vars = array(
            'template_name' => ee()->TMPL->template_name,
            'template_group' => ee()->TMPL->group_name,
            'template_id' => ee()->TMPL->template_id,
            'template_type' => (ee()->TMPL->embed_type) ? ee()->TMPL->embed_type : ee()->TMPL->template_type
        );

        $this->return_data = ee()->TMPL->parse_variables(ee()->TMPL->tagdata, array($vars));
    }

    // --------------------------------------------------------------------

    /**
     * Usage
     *
     * Plugin Usage
     *
     * @access  public
     * @return  string
     */
    public function usage()
    {
        return <<<YODA
        Parses a few metadata variables about the template currently being processed.

        {exp:template_info}
            This is the {template_name} template from the {template_group} group.
            Its ID is {template_id} and its type is {template_type}.
        {/exp:template_info}
YODA;
    }

    // --------------------------------------------------------------------

}
// END CLASS

/* End of file pi.template_info.php */
/* Location: ./system/expressionengine/third_party/template_info/pi.template_info.php */
2
  • Nicely done, sir. Commented May 7, 2014 at 1:04
  • Hi, Derek! Why not to add this to github of EllisLab? Thanks!
    – Sobral
    Commented Jul 20, 2014 at 1:45
2

Well if you're not using embeds for the template then you could use segment_1 and segment_2. However I do not believe what you want to do is possible.

2

It looks like Leevi Graham did something like this for EE1. I'm not sure how well it would work with Structure or other custom URL structures. I suppose you could look into doing something similar for EE2.

However, I'd recommend considering an alternative approach. I'm not sure why you're needing to set the html ID to the template name, but maybe you can use something else here. If you're including the header via an embed or a Stash template partial, you could specify this per template as you're including it (like you would the page title). I believe there would even be a way to trick it when using snippets, as well.

1
  • Thanks Jim. I'm just trying to remove the administrative burden hardcoding the template name even via a {embed:var} causes. I do understand it's quite trivial to add in most instances but I always forget. I should note it's pretty common for us to code CSS specific to an EE template and that's the reason for this. Commented Jan 24, 2013 at 18:17
0

I understand there has been a variable {template_name} for some time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.