Jump to content

Don't break "double words" when new line


Gomlers

Recommended Posts

Hi!

 

First post, beginner in PHP - please be nice :)

 

I have a webshop with tags on the left column. The shop is not open to public yet, so unfortunately I can't pass a link to it.

 

The left-column isn't wide enough to show all the tags on one line, so they are splitted into separate lines.

 

I don't want the last tag on each line to be separated - as in burgundy widgets in my example:

 

green widgets red widgets

blue widgets burgundy

widget

 

I want it like this:

green widgets red widgets

blue widgets

burgundy widgets

 

Prestashop uses smarty to generate the html in the .tpl files, so I guess I need to write a function that fixes this in tags.php and then put something from that php-function into the tags.tpl file?

 

I think this is an example of what I'm trying to do:

$longString = 'I like apple. You like oranges. We like fruit. I like meat, also.';

$arrayWords = explode(' ', $longString);

// Max size of each line
$maxLineLength = 18;

// Auxiliar counters, foreach will use them
$currentLength = 0;
$index = 0;

foreach($arrayWords as $word)
{
// +1 because the word will receive back the space in the end that it loses in explode()
$wordLength = strlen($word) + 1;

if( ( $currentLength + $wordLength ) <= $maxLineLength )
{
$arrayOutput[$index] .= $word . ' ';

$currentLength += $wordLength;
}
else
{
$index += 1;

$currentLength = $wordLength;

$arrayOutput[$index] = $word;
}
}

... but I don't know how to get it right with smarty and everything. Any help would be appreciated, as I'm trying to learn :)

 

Link to comment
Share on other sites

Ok - I just used that code as an example, I found it stackoverflow.

Sorry, than that is NOT what I want :) I'm a complete beginner to php!

 

I would of course like to explode by the actual tags, not the blank spaces in between which ever word..

 

Can I do something with the <a href=""> that is wrapped around every tag?

Edited by Gomlers
Link to comment
Share on other sites

Okay, so in your script, how are the tags currently stored? For example, if you already have them in an array, you can do this:

// example array of tags
$tags = array(
  'tag',
  'tag',
  'another tag'
);
$array = array_map(
  function($v){return str_replace(' ',' ',$v);},
  $tags
);
Basically the goal is to replace the spaces with  .
Link to comment
Share on other sites

Wow, you are fast! Thank you.

 

This is how the function in the tag.php looks like:

    function hookLeftColumn($params)
    {
        $tags = Tag::getMainTags((int)($params['cookie']->id_lang), (int)(Configuration::get('BLOCKTAGS_NBR')));
        
        $max = -1;
        $min = -1;
        foreach ($tags as $tag)
        {
            if ($tag['times'] > $max)
                $max = $tag['times'];
            if ($tag['times'] < $min || $min == -1)
                $min = $tag['times'];
        }
        
        if ($min == $max)
            $coef = $max;
        else
        {
            $coef = (BLOCKTAGS_MAX_LEVEL - 1) / ($max - $min);
        }
        
        if (!sizeof($tags))
            return false;
        foreach ($tags AS &$tag)
            $tag['class'] = 'tag_level'.(int)(($tag['times'] - $min) * $coef + 1);
        $this->smarty->assign('tags', $tags);

        return $this->display(__FILE__, 'blocktags.tpl');
    }

Wish I could paste my new great example code that almost works below, but I'm really confused :(

 

Instead, here is the blocktags.tpl the way it is originally from prestashop

{if $tags}
    {foreach from=$tags item=tag name=myLoop}
        <a href="{$link->getPageLink('search', true, NULL, "tag={$tag.name|urlencode}")|escape:'html'}" title="{l s='More about' mod='blocktags'} {$tag.name|escape:html:'UTF-8'}" class="{$tag.class} {if $smarty.foreach.myLoop.last}last_item{elseif $smarty.foreach.myLoop.first}first_item{else}item{/if}">{$tag.name|escape:html:'UTF-8'}</a>
    {/foreach}
{else}
    {l s='No tags have been specified yet.' mod='blocktags'}
{/if}
Edited by Gomlers
Link to comment
Share on other sites

well, based on the posted code, perhaps in tag.php, you can change this:

 

        foreach ($tags AS &$tag)
            $tag['class'] = 'tag_level'.(int)(($tag['times'] - $min) * $coef + 1);
To this:

 

        foreach ($tags AS &$tag) {
            $tag['class'] = 'tag_level'.(int)(($tag['times'] - $min) * $coef + 1);
            $tag['name'] = str_replace(' ',' ',$tag['name']);  
        }
Link to comment
Share on other sites

That actually almost worked :)

The tags came on separate lines if they are too long, but the   is shown between the words.

 

Like blue widgets white widgets and so on..

 

But I feel you got me closer to the solution :D

I'm trying to understand why...

Link to comment
Share on other sites

I had a feeling that might happen.. I figured the smarty code might somehow escape it or htmlentities it.

 

Perhaps remove what you just changed and instead, try changing your smarty template code to this:

 

adding |replace:' ':' '

{if $tags}
    {foreach from=$tags item=tag name=myLoop}
        <a href="{$link->getPageLink('search', true, NULL, "tag={$tag.name|urlencode}")|escape:'html'}" title="{l s='More about' mod='blocktags'} {$tag.name|escape:html:'UTF-8'}" class="{$tag.class} {if $smarty.foreach.myLoop.last}last_item{elseif $smarty.foreach.myLoop.first}first_item{else}item{/if}">{$tag.name|escape:html:'UTF-8'|replace:' ':' '}</a>
    {/foreach}
{else}
    {l s='No tags have been specified yet.' mod='blocktags'}
{/if}
Unfortunately, I don't have much experience with smarty, so it's just a guess based on a quick google search.
Link to comment
Share on other sites

Well, your google search and guesswork did very well!

 

It looks perfect now.. Thank you very much Josh!

 

What a great forum-experience this was. Problem solved in less than an hour! :D

 

Is this because

foreach variable as variable

puts all the tags in an array, so the only thing the script needed, was to output the tag "red widgets" as a combined html-code red widgets which wouldn't break?

Edited by Gomlers
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.