2

I'm building a website with with one template but several styles. I need each style to have it's own logo, and a customized link for the logo. The template is based on the Gantry Framework.

Is it possible to detect the current template style (name or ID), and use an if-else statement in the index.php file? This is roughly what I'm trying to do:

<?php
  if (styleid = "1") {
    $link = "/";
    $logo = "logo1.jpg";
  } elseif (styleid = "2" {
    $link = "/otherlink";
    $logo = "logo2.jpg";
  }
  echo '<a href="'.$link.'"><img src="/images/'.$logo.'"></a>';
?>
  • Did you try it? Does it work? – elk Sep 11 '14 at 15:51
  • Mate did you try any of the suggestions on my answer? Curious to know about the second approach... – FFrewin Sep 12 '14 at 18:43
  • @FFrewin, sorry I haven´t had time yet. But thanks for the suggestion, it looks promissing and I´ll let you know how it works. The second approach seems to be the best option in this case. – johanpw Sep 12 '14 at 21:52
1

With Gantry, you can have different logos for each style. But this will not let you have different links.

Although there can be various approaches to achieve what I am assuming you have in mind, to answer directly to your question on how to get the template style assigned to a menu item, you could use something like this:

Getting Template Style:

$styleId = JMenu::getInstance('site')->getActive()->template_style_id;

This will return the ID of the assigned style on the active menu. Note that the default style will return 0.

I have tested this on a gantry template and it seems to work.


A second approach

Using Gantry Framework to create custom templates, you could add an extra setting (custom element field to extend the logo feature) in the template administration, for the custom link of the logo.

So all you need to do, is to add an extra field in the template_options.xml file and get this value in your template's index, and translate it to a link. You could use a menu-items field, or a database field querying for the menu-items links, so it will make it easier for the user to select a internal site link to another menu-item.

Your comment about the issue:

I have tested your second approach, and created a custom field in templateOptions.xml like this: But when I try to use the value in features/logo.php, it's empty: $customlink = $gantry->get('customlogo'); ($customlink contains no data). Any ideas?

Answer:

The templateOptions.xml has the following code for the Logo Feature:

<fields name="logo" type="selectedset" label="LOGO" description="LOGO_DESC">
    <field name="enabled" type="toggle" default="1" label="SHOW" />
    <field name="position" type="position" translation="false" default="header-a" label="POSITION" />
    <field name="type" type="selectbox" selector="true" default="gantry" label="TYPE" isbodyclass="true" setbyurl="true" setinsession="true" setbysession="true" setincookie="true" setbycookie="true"/>
    <fields type="set" name="gantry" label="Gantry Template">
    </fields>
    <fields type="set" name="custom" label="CUSTOM">
        <field name="image" type="imagepicker" default="" label="LOGO_PICKER" description="LOGO_PICKER_DESC" setinsession="true" setbysession="true" setincookie="true" setbycookie="true" />
    </fields>
</fields>

You most likely added your custom field inside the deeper <fields - groups>?

I noticed that if you add a custom field this way, then it's not accessible in the feature.php with the $this->get('fieldname').

So, you should add the custom field like below:

<fields name="logo" type="selectedset" label="LOGO" description="LOGO_DESC">
    <field name="enabled" type="toggle" default="1" label="SHOW" />
    <field name="position" type="position" translation="false" default="header-a" label="POSITION" />
    <field name="type" type="selectbox" selector="true" default="gantry" label="TYPE" isbodyclass="true" setbyurl="true" setinsession="true" setbysession="true" setincookie="true" setbycookie="true"/>
    <fields type="set" name="gantry" label="Gantry Template">
    </fields>
    <fields type="set" name="custom" label="CUSTOM">
        <field name="image" type="imagepicker" default="" label="LOGO_PICKER" description="LOGO_PICKER_DESC" setinsession="true" setbysession="true" setincookie="true" setbycookie="true" />
    </fields>
    <field name="mylogolink" type="text" default="" label="logo link" description="logo link" />
</fields>

Then you probably will be able to access it through $this->get('mylogolink');

  • I have tested your second approach, and created a custom field in templateOptions.xml like this: <fields name="logolink" type="selectedset" label="Logo Link" description="Custom link"><field name="customlink" type="text" label="Custom link" description="" /></fields> But when I try to use the value in features/logo.php, it's empty: $customlink = $gantry->get('customlogo'); ($customlink contains no data). Any ideas? – johanpw Oct 7 '14 at 15:52
  • check update mate – FFrewin Oct 7 '14 at 17:18
  • Perfect, thank you. – johanpw Oct 7 '14 at 18:56
2

This is just an idea and I know it doesn't directly answer your coding issue, but what if you were to use NoNumbers Advanced Module Manager to create a separate module for each template.

You could then assign that specific module to it's corresponding template? Via the AMM admin settings area.

It means you would have to create a different module (URLs etc) for each template though, but that shouldn't be too much work as you could just duplicate each module.

1

In case someone's interested, this is how to put everything together:

In the file templateOptions.xml I added this code (as suggested by FFrewin):

<field name="mylogolink" type="text" default="" label="logo link" description="logo link" />

Then in /features/logo.php, replace this:

<div class="rt-logo-block">
    <a href="<?php echo $gantry->baseUrl; ?>" id="rt-logo"></a>
</div>

With this:

<div class="rt-logo-block">
   <a href="<?php echo ($customlink != "" ? $this->get('mylogolink') : $gantry->baseUrl;) ?>" id="rt-logo"></a>
</div>

It simply checks if $customlink is empty or not. If it's empty, the default home link is used, if not, the given link is used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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