Hi,
I have concocted a new plugin called “Subsegment”.
It allows you to split url segment into subsegments array and find any subsegment.
The tag
{exp:subsegment}
has 5 parameters:
1) segment - (integer from 1 to 9). Required. Allows you to specify which url segment will
be parsed. Numbering of segments is the same as in ExpressionEngine’s url segment variables.
2) separator - (any string which consists from characters legal for use in url segment). Required.
Allows you to specify the string according to which url segment will be split into array of subsegments.
3) index - (0 or integer bigger than 0). Optional. Allows you to specify which member
of the array of subsegments will be returned.
4) on_failure - (‘segment’ or any string which consists from characters legal for use
in ExpressionEngine’s tag parameter values). Optional. Allows you to specify what
{exp:subsegment} tag will output in case needed subsegment was not found.
5) invalid_input - Optional. Accepts two values: “alert” and “silence”.
Default value is “silence”. If the value is “alert”, then in cases when some
parameter’s value is invalid plugin exits and PHP alert is being shown;
if the value is “silence”, then in cases when some parameter’s value
is invalid plugin finishes its work without any alert being shown.
Set this parameter to “alert” for development, and to “silence” - for deployment.
Example of usage:
If segment_3 is
/project_5-article_7-section_6/
then you can use optionally either single tag as here
{exp:subsegment segment="3" separator="-" index="1"}
or variable {subsegment} within {exp:subsegment}{/exp:subsegment} tag pair
{exp:subsegment segment="3" separator="-" index="1"}
{subsegment}
{/exp:subsegment}
or use {subsegment_0}, {subsegment_1} etc. variable within
{exp:subsegment}{/exp:subsegment} tag pair
{exp:subsegment segment="3" separator="-"}
{subsegment_1}
{/exp:subsegment}
All three will return “article_7”.
Variables {subsegment} and {subsegment_0}-{subsegment_8}
within {exp:subsegment}{/exp:subsegment} tag pair is
more powereful because you can it’s output as parameter of other tag. For such use
you must add parse=“inward” parameter to {exp:subsegment} tag.
For example, code as this will work
{exp:subsegment segment="3" separator="-" index="1" parse="inward"}
{exp:weblog:entries weblog="my_weblog" url_title="{subsegment}"}
Some code
{/exp:weblog:entries}
{/exp:subsegment}
Alternatively you can use this code
{exp:subsegment segment="3" separator="-" parse="inward"}
{exp:weblog:entries weblog="my_weblog" url_title="{subsegment_1}"}
Some code
{/exp:weblog:entries}
{/exp:subsegment}
If subsegment does not exist then {exp:subsegment} tag by default will return nothing.
In case ‘on_failure’ parameter was defined, then, if subsegment does not exist,
the tag will output what was specified as value of this parameter.
If ‘segment’ is the value of ‘on_failure’ parameter, then the tag will output
segment, if segment exists. If ‘on_failure’ parameter has some other string as
its value, then that string will be outputted.
Place this tag in any of your templates.
Any comments and suggestions appreciated.
Changelog:
-1.4 // Added invalid_input parameter.
-1.3.1 // Fixed bug - string specified as a value of “on_failure” parameter not being displayed in case needed segment not found.
-1.3 // Index parameter made optional. New variables {subsegment_0}-{subsegment_8} introduced.
-1.2.1 // Fixed bug - on_failure parameter causes result to be displayed as output of entire {exp:subsegment} tag instead of as output of {subsegment} variable.
-1.2 // Added on_failure parameter.
-1.1 // Added optional use of either single {exp:subsegment} tag or variable {subsegment} within {exp:subsegment}{/exp:subsegment} tag pair.
-1.0 // First release
