Jump to content

Conditional array keys with smarty


Truemedia

Recommended Posts

Hi, I'm not completely new to smarty although I have so far only done very simple stuff. I am trying to add css dynamically through inline styling in html attributes (example My words).

I already have a script setup to automatically deal with some default styling with an array but I want either the first or last key of the array to have modified width.


   {if $url}
{$title}
   {else}
       {$title}
   {/if}


   {foreach from=$blocklink_links item=blocklink_link}
{$blocklink_link.$lang}
   {/foreach}



I realize people will suggest use css stylesheets but inline styling is most suited for the application of this code. Is there any smary equivalent to:

foreach($array as $key => $value)
{
if($key == "speacialKey")
{
 <html></html>
}
}



I tried posting this in the smarty forum but anywhere I try to post I get "The topic or post you requested does not exist" >:( WHY?

Any help appreciated thanks

Link to comment
Share on other sites

Cheerz :) , although while I was messing around I tried hooking a header css file and it didn't show up.

Any idea why it doesn't work? This is the code I've code for tryna add a css file

public function install()
   {
        if (parent::install() == false OR $this->registerHook('header') == false OR $this->registerHook('top') == false)
            return false;
       $query = 'CREATE TABLE '._DB_PREFIX_.'blocklink (`id_link` int(2) NOT NULL AUTO_INCREMENT, `url` varchar(255) NOT NULL, new_window TINYINT(1) NOT NULL, PRIMARY KEY(`id_link`)) ENGINE=MyISAM default CHARSET=utf8';
        if (!Db::getInstance()->Execute($query))
            return false;
        $query = 'CREATE TABLE '._DB_PREFIX_.'blocklink_lang (`id_link` int(2) NOT NULL, `id_lang` int(2) NOT NULL, `text` varchar(64) NOT NULL, PRIMARY KEY(`id_link`, `id_lang`)) ENGINE=MyISAM default CHARSET=utf8';
        if (!Db::getInstance()->Execute($query))
            return false;
        return (Configuration::updateValue('PS_BLOCKLINK_TITLE', array('1' => 'Block link', '2' => 'Bloc lien')) AND Configuration::updateValue('PS_BLOCKLINK_TITLE', ''));
   }



function hookHeader($params)
   {
       global $smarty, $cookie;
       $smarty->assign(array(
           'menuCssUrl' => 'http://'.Tools::getHttpHost(false, true).__PS_BASE_URI__.'modules/'.$this->name.'/css/mainmenu.css',
       ));
       return $this->display(__FILE__, 'header.tpl');
   }

Link to comment
Share on other sites

The Smarty equivalent of:

foreach($array as $key => $value)
  if($key == "specialKey")
    <html></html>



is:

{foreach from=$array key=key item=value}
  {if $key == "specialKey"}
     <html></html>
  {/if}
{/foreach}



You can also get the first item without knowing the key like this:

{foreach from=$array item=value name=loop}
  {if $smarty.foreach.loop.first}
     <html></html>
  {/if}
{/foreach}



and the following to get the last item without knowing the key:

{foreach from=$array item=value name=loop}
  {if $smarty.foreach.loop.last}
     <html></html>
  {/if}
{/foreach}



You can give the foreach loop any name you like.

Link to comment
Share on other sites

Thanks guys, this the code I was trying to use to load css file but didn't work.

This is from the module hook:

function hookHeader($params)
   {
       global $smarty, $cookie;
       $smarty->assign(array(
           'menuCssUrl' => 'http://'.Tools::getHttpHost(false, true).__PS_BASE_URI__.'modules/'.$this->name.'/css/mainmenu.css',
       ));
       return $this->display(__FILE__, 'header.tpl');
   }



then this is the header tpl file:

<link rel="stylesheet" type="text/css" href="{$menuCssUrl}css/mainmenu.css" />



and this is the install function of the module:

public function install()
   {
        if (parent::install() == false OR $this->registerHook('header') == false OR $this->registerHook('top') == false)
            return false;
       $query = 'CREATE TABLE '._DB_PREFIX_.'blocklink (`id_link` int(2) NOT NULL AUTO_INCREMENT, `url` varchar(255) NOT NULL, new_window TINYINT(1) NOT NULL, PRIMARY KEY(`id_link`)) ENGINE=MyISAM default CHARSET=utf8';
        if (!Db::getInstance()->Execute($query))
            return false;
        $query = 'CREATE TABLE '._DB_PREFIX_.'blocklink_lang (`id_link` int(2) NOT NULL, `id_lang` int(2) NOT NULL, `text` varchar(64) NOT NULL, PRIMARY KEY(`id_link`, `id_lang`)) ENGINE=MyISAM default CHARSET=utf8';
        if (!Db::getInstance()->Execute($query))
            return false;
        return (Configuration::updateValue('PS_BLOCKLINK_TITLE', array('1' => 'Block link', '2' => 'Bloc lien')) AND Configuration::updateValue('PS_BLOCKLINK_TITLE', ''));
   }



Any ideas? IF I can fix this today I will be able to sell my menu almost straight away and introduce a css editor in a future update.

Link to comment
Share on other sites

Also, your variable is being set to:

'http://'.Tools::getHttpHost(false, true).__PS_BASE_URI__.'modules/'.$this->name.'/css/mainmenu.css'


and you are trying to load:

"{$menuCssUrl}css/mainmenu.css"


You already have "css/mainmenu.css" in the variable, why you appending it to the variable in the tpl?
I think I also see an extra comma in the assignment, the comma after '/css/mainmenu.css' is not needed:

Link to comment
Share on other sites

Nah still doesn't work, this is the source from the head when viewing store front:

<head>
       <title>MCO Store</title>
       <meta name="description" content="Shop powered by PrestaShop" />
       <meta name="keywords" content="shop, prestashop" />
       <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
       <meta name="generator" content="PrestaShop" />
       <meta name="robots" content="index,follow" />
       <link rel="icon" type="image/vnd.microsoft.icon" href="http://localhost/prestashop/img/favicon.ico" />
       <link rel="shortcut icon" type="image/x-icon" href="http://localhost/prestashop/img/favicon.ico" />
       <link href="/prestashop/themes/prestashop/css/global.css" rel="stylesheet" type="text/css" media="all" />
           [removed][removed]
       [removed]
           var baseDir = 'http://localhost/prestashop/';
           var static_token = '440a98dc7f29b71e391b2a7aeb0621ff';
           var token = 'fffc8556fea84997399b555738cfc40a';
           var priceDisplayPrecision = 2;
           var roundMode = 2;
       [removed]
       [removed][removed]
       [removed][removed]
       [removed][removed]
       <!-- Block search module HEADER -->
<link rel="stylesheet" type="text/css" href="http://localhost/prestashop/css/jquery.autocomplete.css" />
[removed][removed]
<!-- Block search module HEADER --><link rel="alternate" type="application/rss+xml" title="MCO Store" href="http://localhost/prestashop/modules/feeder/rss.php?id_category=0&orderby=position&orderway=ASC" />
   </head>



No link rel there of the one its supposed to add.

Link to comment
Share on other sites

Iv'e got a lot of things working such as automatic size adjustment before smarty and the if statements working in the tpl, but it seems to be actually sending out my links backwards.

Should be link1, link2, link3 ect
But instead it's link3, link2, link1

This is probably cause of how smarty is working needs to go backwards through the array, but is there a way of reversing this code to show the links in the right order?

{foreach from=$blocklink_links item=blocklink_link name=loop}
       {if $smarty.foreach.loop.first}
{$blocklink_link.$lang}
       {elseif $smarty.foreach.loop.last}
{$blocklink_link.$lang}
       {else}
{$blocklink_link.$lang}
       {/if}
   {/foreach}



Thanks

Link to comment
Share on other sites

  • 7 months later...
  • 8 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...