Jump to content

{{if \IPS\Request::i()->path == 'slug/one-extra-bit'}}


Meddysong

Recommended Posts

Posted

I have a custom block in Pages which shows the latest records for a particular category. Unfortunately, if you drag and drop, that block features on every page that the database appears on; category view, display and listing. That's not what I want. There's no point having it on listing or the record display.

I know of one solution and its working:

{{if \IPS\Request::i()->path == 'holidays'}}{block="our_latest_adventures"}{{endif}}

That adds the block to my category listing (which is found at http://timeofftakeoff.com/holidays/).

What I'd like to do is add it a step deeper to my categories mysite.com/holidays/2011, mysite.com/holidays/2012 etc.

Obviously I could write it as

{{if \IPS\Request::i()->path == 'holidays' || \IPS\Request::i()->path == 'holidays/2012' || \IPS\Request::i()->path == 'holidays/2013'}}{block="our_latest_adventures"}{{endif}}

and so on but that's going to get quite lengthy and it's triggering me because there must be a neater solution.

Can anybody think of a way to rewrite it such that in one statement we get 'holidays/%' but not 'holidays/%/xyz'? By that I mean anything which goes one level past 'holidays' but doesn't include anything which goes further. (So, http://timeofftakeoff.com/holidays/2014/ is OK but http://timeofftakeoff.com/holidays/2014/spain-2014/ and http://timeofftakeoff.com/holidays/2014/spain-2014/day-9-madrid/ aren't.)

Does anybody have any thoughts?

Posted

You can explode the path var and check count of elements

{{$pieces = explode('/', \IPS\Request::i()->path);}}
{{if $pieces[0] == 'holidays' and count($pieces) <= 2}}{block="our_latest_adventures"}{{endif}}

 

Posted

Clever man - thank you!

There's an additional problem that I've only just realised: I would like this block to appear in the sidebar. If I paste this code into the page or into the database template, it will appear as part of the article body but not in the side bar. Is there a clever solution for that?

Posted

Why you don't use Manager blocks?

2 hours ago, Meddysong said:

If I paste this code into the page

Try add

{{$pieces = explode('/', \IPS\Request::i()->path);}}
{{if $pieces[0] == 'holidays' and count($pieces) <= 2}}
	{{\IPS\Output::i()->sidebar['contextual'] = \IPS\cms\Blocks\Block::display('our_latest_adventures');}}
{{endif}}

 

Posted
17 minutes ago, Meddysong said:

Do I need to add a parameter somewhere to force it to be vertical? If so, how and where?

\IPS\cms\Blocks\Block::display('our_latest_adventures', 'vertical')

 

Posted

It's easy when you know how :)

The final problem is that it doesn't have any of the formatting (ie for the section title) that the usual widgets have. Is that too much to hope for. (It's not the end of the world if it is. At least I have the data!)

Posted
On 13.07.2017 at 6:40 PM, Meddysong said:

The final problem is that it doesn't have any of the formatting (ie for the section title) that the usual widgets have. Is that too much to hope for. (It's not the end of the world if it is. At least I have the data!)

Widgets and contextual menu have different markup

Try

{{$block = null; try {$block = \IPS\cms\Blocks\Block::load('our_latest_adventures', 'block_key');} catch(\Exception $e) {} }}
{{if $block and \IPS\cms\Blocks\Block::display($block, 'vertical')}}
	{{\IPS\Output::i()->sidebar['widgets']['sidebar'][] = $block->widget();}}
{{endif}}

 

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...