Post by blizeH » Thu Aug 08, 2013 3:43 am

Hi,

I'd like to add in a link to my blog at the very end of the navigation in header.tpl, here's the code:

Code: Select all

  <ul class="clearfix">
    <?php foreach ($categories as $category) { ?>
    <li<?php if ($category['children']) {?> class="parent"<?php } ?>><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
      <?php if ($category['children']) { ?>
      <div>
		<?php if (isset($category['thumb'])) {?><div class="img"><img src="<?php echo $category['thumb'];?>"></div><?php }?>  
        <?php for ($i = 0; $i < count($category['children']);) { ?>
        <ul>
          <?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
          <?php for (; $i < $j; $i++) { ?>
          <?php if (isset($category['children'][$i])) { ?>
          <li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
          <?php } ?>
          <?php } ?>
        </ul>
        <?php } ?>
      </div>
      <?php } ?>
    </li>
    <?php } ?>
  </ul>
Ideally I'd like it just before the </ul> at the end, but how would I target that using vQmod? I can't put it after <?php } ?> nor before </ul> because they're both quite common

Is there an alternative way?

Many thanks

New member

Posts

Joined
Sat Jan 14, 2012 4:20 am

Post by Qphoria » Thu Aug 08, 2013 4:03 am

Of course.. there are many ways... only limited to your own creativity

1. Indexing. You could specify which <ul> tag to match.

Code: Select all

<search position="after" index="13"><![CDATA[
<ul>
]]></search>
NOT RECOMMENDED since if the code changes, it would match the wrong one

2. Offset. Find a unique point in the code and offset how many lines away you need. In this case the </div> tag seems pretty unique so you could do

Code: Select all

<search position="after" offset="4"><![CDATA[
</div>
]]></search>
BETTER, BUT NOT GREAT. Still a chance for hitting the wrong tag because </div> is so ambiguous

3. Find out what comes AFTER this code and use the before tag. In fact, if you look at the header.tpl code you'll see this is the next line after that snippet above:

So now you can use that with the "BEFORE" position and a slight offset. This at least gives a more solid anchor point for the offset

Code: Select all

<search position="before"><![CDATA[
<?php if ($error) { ?>
]]></search>
MORE PREFERRED

4. You could also add some jQuery to add the menu entry at the bottom.

Code: Select all

<search position="before"><![CDATA[
?>
]]></search>

Code: Select all

<add><![CDATA[
<script type="text/javascript">
$(document).ready(function() {
    $('#menu ul').append('<li>something here</li>');
});
</script>
]]></add>
ALSO PREFERRED and probably the method I would personally use for this.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by blizeH » Thu Aug 08, 2013 7:04 am

Thank you so much! Unfortunately the tags after the </ul> are all </div> closing tags, so I'll need to use the offset function :)

I like the idea of jQuery also, but whilst editing the PHP directly is an option (using offset) I think I'll go for that

Thanks again for the great guide! Funny that just yesterday I was completely adverse to installing vQmod because I didn't want to 'install' anything on the server (what?) but now just a day later I'm a complete convert - it's fantastic and a MUCH better way of doing things than editing core files like I was previously!

New member

Posts

Joined
Sat Jan 14, 2012 4:20 am

Post by blizeH » Mon Aug 12, 2013 4:51 am

Hi,

Just to make sure I'm on the right track with this, to remove the model sorts from the category page:

Code: Select all

			if ($this->config->get('config_review_status')) {
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_rating_desc'),
					'value' => 'rating-DESC',
					'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=rating&order=DESC' . $url)
				); 
				
				$this->data['sorts'][] = array(
					'text'  => $this->language->get('text_rating_asc'),
					'value' => 'rating-ASC',
					'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=rating&order=ASC' . $url)
				);
			}
			
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_model_asc'),
				'value' => 'p.model-ASC',
				'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=p.model&order=ASC' . $url)
			);

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_model_desc'),
				'value' => 'p.model-DESC',
				'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=p.model&order=DESC' . $url)
			);
Is this the best way?

Code: Select all

      <search position="replace" offset="14">
        <![CDATA[
			'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=rating&order=ASC' . $url)
        ]]>
      </search>
      <add>
        <![CDATA[
				);
			}
        ]]>
      </add>

New member

Posts

Joined
Sat Jan 14, 2012 4:20 am

Post by Qphoria » Tue Aug 13, 2013 5:04 am

You really never want to use offset if you don't have to. It is a work around used in extreme cases, but not a recommended method.

For this case, I would personally leave that code alone and at the end of all the sorts, I would just remove the ones I don't want. Yes it means you are basically setting something and then unsetting it, but it keeps the code clean and more future resistant.

I would likely just add something at the end like:

Code: Select all

       <search position="before"><![CDATA[
$pagination = new Pagination();
        ]]></search>


      <add><![CDATA[
foreach ($this->data['sorts'] as $k => $v) {
    if ($v['value'] == 'p.model-ASC' || $v['value'] == 'p.model-DESC') {
        unset($this->data['sorts'][$k]);
    }
}
        ]]></add>
I'm using the pagination line as my search tag because that is a lower point in the file than the array setting, and is unique enough to be a single instance anchor. Then I simply add a quick for loop to loop through the sorts and remove any that I don't want.

This keeps it cleaner for others as well. Some mods may add additional sort options, so this prevents them from breaking... and the performance of a simple array set/unset is so minuscule that there is no worry of a performance hit.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by blizeH » Thu Aug 15, 2013 12:34 am

That's a great idea, thank you Qphoria! Like you said it's much tidier, and should play nicely with any other mods too :)

(and thanks again for the great Ubercheckout extension, and helping me with my $column_left problem!)

New member

Posts

Joined
Sat Jan 14, 2012 4:20 am
Who is online

Users browsing this forum: No registered users and 1 guest