plugin: Subsegment
Posted: 08 May 2008 02:30 AM   [ Ignore ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  877
Joined  02-02-2007

This plugin allows you to split url segment into subsegments array and find any subsegment.

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) 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 variables of {exp:subsegment} tag will output in case needed subsegment was not found.

EXAMPLE OF USAGE:

If segment_3 is

/project_5-article_7-section_6/

then you can use {subsegment_0}, {subsegment_1} etc.  variable within
{exp:subsegment}{/exp:subsegment} tag pair

{exp:subsegment segment="3" separator="-"}
{subsegment_1}
{
/exp:subsegment}

It will return “article_7”.

Variables {subsegment_0}-{subsegment_8}
within {exp:subsegment}{/exp:subsegment} tag pair can be used
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="-" 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 the 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.

Conditionals are supported. The value of “on_failure” parameter has no effect
on the values of variables {subsegment_0}-{subsegment_8} inside conditionals.
E.g. if the value of “on_failure” parameter is “AAA” and the third subsegment
does not exist, then the variable {subsegment_2} will output “AAA” outside conditionals
and empty string inside conditionals.

Changelog:

-1.5 // Ditched support for {subsegment} variable; added support for conditionals.
-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

File Attachments
pi.subsegment_v1.5.zip  (File Size: 3KB - Downloads: 166)
 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 08 May 2008 02:44 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  28986
Joined  05-15-2004

So, what’s the advantage of using such a sub-segment instead of regular segments? I really am curious, were you running into limitations with the permitted number, 9? What particular itch were you scratching here, so to speak?

Perhaps you should call the parameter “separator” or something, instead of simply “string”? Also, I probably wouldn’t want to use * or ~ or #, but is there a reason to disallow “|” or “:” as separators?

 Signature 

ExpressionEngine 2.0 Downloads | Docs | Bugtracker

“If the English language made any sense, lackadaisical would have something to do with a shortage of flowers.” (Doug Larson)

Profile
MSG
 
 
Posted: 08 May 2008 02:46 AM   [ Ignore ]   [ # 2 ]  
Professor
Avatar
RankRankRankRankRankRankRank
Total Posts:  10690
Joined  04-15-2006

Hiya,

Sounds like an interesting little plugin but I’m having a hard time thinking of a use for it at the moment. Why would you want to take a segment and then get part of it? Is there some way of using segments that I am missing somewhere. Don’t suppose you could post a quick real-world example just to get my thick skinned head around this could you?

Thanks for creating it though, I’m sure that when I totally understand the uses for it I will be using it!! wink

Best wishes,

Mark

 Signature 

Shopping Cart Plugin
Full list of add-ons
———————————————————-
Buy me a drink, or two if you like!!

Profile
 
 
Posted: 08 May 2008 05:43 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  877
Joined  02-02-2007

Hi Ingmar and Mark,

Thanks for comments and suggestions.

Really, naming parameter “separator” makes much better sense than naming it “string”.

Concerning chars included in separator strings: it seemed to me that some validation is needed and some chars should be considered as illegal. Which chars should be illegal, I was not sure and included in the list of illegal chars those which I never saw used in url segments and would not use by myself. Maybe somebody could propose some consistent list of such chars?

Now I have uploaded version 1.01 of Subsegment plugin in which “string” parameter is changed into “separator” and both “:” and “|” chars are removed from illegal chars list.

I have anticipated that the idea for such plugin may seem strange in the situation when EE supports even 9 url segment variables. Well, let’s consider this real world situation.

There are on the website several similar publishing projects. Each project uses the same set of templates and weblogs. Each page beyond page-specific data displays some data about project. Titles and url_titles are for most entries are standard, such as “article_1”, “articles_5_7” etc.; entries which belong to different projects may have the same title. Since url_titles should be unique and human-friendly, there is a need for some consistent policy in giving url_titles; to ensure uniqueness of url_titles by appending random chars is not acceptable. It seems that quite natural in this situation is to append project name to each url_title, so that instead “articles_5_7” we would have something like “project_name-articles_5_7”. And since each page should display some data about the project, it seems quite natural that information which project it should be should be parsed from url_title.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 08 May 2008 06:24 AM   [ Ignore ]   [ # 4 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  28986
Joined  05-15-2004

Yes, some characters are illegal in URLs, but you still might come across them and seaparate segments by that character. The colon could be useful for working with a wiki, eg, and the vertical bar is used by some APIs I know of, like the Google Charts API.

 Signature 

ExpressionEngine 2.0 Downloads | Docs | Bugtracker

“If the English language made any sense, lackadaisical would have something to do with a shortage of flowers.” (Doug Larson)

Profile
MSG
 
 
Posted: 18 May 2008 01:02 PM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  877
Joined  02-02-2007

I have updated the plugin Subsegment. You can download version 1.1 using the link on top of the page.

The changes are as follows.

Now you can use optionally either single {exp:subsegment} tag or variable {subsegment} within {exp:subsegment}{/exp:subsegment} tag pair. In both cases the output will be the same.

The use of variable {subsegment} within {exp:subsegment}{/exp:subsegment} tag pair is more powerful because you can use 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}

As allways, any suggestions appreciated.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 29 May 2008 02:11 AM   [ Ignore ]   [ # 6 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  877
Joined  02-02-2007

I have updated the plugin Subsegment. You can download version 1.2 using the link on top of the page.

In this version a new parameter - ‘on_failure’ - was added. This parameter is optional and accepts as its value either ‘segment’ or any string which consists from characters legal for use in ExpressionEngine’s tag parameter values. It allows you to specify what
{exp:subsegment} tag will output in case needed subsegment was not found.

If subsegment does not exist the tag by default returns nothing.
In case ‘on_failure’ parameter is defined and ‘segment’ is its value, 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.

Why there is a need for such parameter?

Consider the code as this:

{exp:subsegment segment="2" separator="-" index="0" parse="inward"}
{exp
:weblog:entries weblog="my_weblog" url_title="{subsegment}" dynamic="off"}
<h1>{title}</h1>
{body}
{
/exp:weblog:entries}
{
/exp:subsegment}

If needed subsegment does not exist, then the value of ‘url_title’ parameter of {exp:weblog:entries} tag will be empty string, and {exp:weblog:entries} will output all entries from ‘my_weblog’ weblog. This may not be what you want. Most probably in case needed subsegment does not exist you will want that {exp:weblog:entries} tag would not output anything. This can be achieved by specifying some string as a value of ‘on_failure’ parameter. This value will become the value of ‘url_title’ parameter and {exp:weblog:entries} tag will not output anything.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 29 May 2008 03:36 PM   [ Ignore ]   [ # 7 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  877
Joined  02-02-2007

1.2.1 - bug fix release.

Serious bug fixed: on_failure parameter causes result to be displayed as output of entire {exp:subsegment} tag instead of as output of {subsegment} variable.

Everyone who uses or plays with this plugin is recommended to upgrade.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 21 June 2008 02:01 PM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  877
Joined  02-02-2007

1.3 - new version.

The changes are as follows.

“Index” parameter was made optional.

New variables {subsegment_0} - {subsegment_8} were introduced.

Now {exp:subsegment}{/exp:subsegment} tag pair can output several subsegments. For example, if segment_3 is /project_5-article_7-section_6/ then the code

{exp:subsegment segment="3" separator="-"}
{subsegment_1}
, {subsegment_2}
{
/exp:subsegment}

will output the string “article_7, section_6”.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 11 August 2008 03:24 AM   [ Ignore ]   [ # 9 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  877
Joined  02-02-2007

1.3.1 - bug fix release.

A minor bug was fixed - string specified as a value of “on_failure” parameter not being displayed in case needed segment not found.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 26 August 2008 09:55 AM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  877
Joined  02-02-2007

Hi,

I have released a version 1.4 of Subsegment plugin.

In this release parameter “invalid_input” was added. This parameter 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 the plugin finishes its work without any alert being shown.
Set this parameter to “alert” for development, and to “silence” - for deployment.

Since the plugin does not do comprehensive validation of parameters’ values, you cannot be absolutely sure that in cases no alert was thrown the input was really valid.

You may ask, why parameter “invalid_input” was needed there already “on_failure” parameter being available. The answer is, these parameters have different purposes. Using parameter “invalid_input” you can specify what plugin should do in case it considers some parameter’s value being invalid; using parameter “on_failure” you can specify what plugin should do in case it considers all parameters’ values being valid, but has found nothing to output.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 13 September 2008 04:12 PM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  534
Joined  12-15-2005

Man I really want to use this plugin, but it doesn’t seem to work within ‘{if}’ tags…

 Signature 

System Info: Production

EE Version:  1.67
Build
:       20090320
PHP Version
: 5.2.5
Profile
 
 
Posted: 14 September 2008 02:29 AM   [ Ignore ]   [ # 12 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  877
Joined  02-02-2007

Man I really want to use this plugin, but it doesn’t seem to work within ‘{if}’ tags…

Can you post an example of your code?

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
Posted: 14 September 2008 09:22 AM   [ Ignore ]   [ # 13 ]  
Professor
Avatar
RankRankRankRankRankRankRank
Total Posts:  10690
Joined  04-15-2006

Or better still use the prep_conditionals function.

Prep Conditionals Documentation

Best wishes,

Mark

 Signature 

Shopping Cart Plugin
Full list of add-ons
———————————————————-
Buy me a drink, or two if you like!!

Profile
 
 
Posted: 14 September 2008 09:43 AM   [ Ignore ]   [ # 14 ]  
Professor
Avatar
RankRankRankRankRankRankRank
Total Posts:  10690
Joined  04-15-2006

Yep you’re right it will work but not always recommended due to certain security implications depending on what you are doing exactly.

Might be alright in this case though.

Best wishes,

Mark

 Signature 

Shopping Cart Plugin
Full list of add-ons
———————————————————-
Buy me a drink, or two if you like!!

Profile
 
 
Posted: 14 September 2008 10:56 AM   [ Ignore ]   [ # 15 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  534
Joined  12-15-2005

Hey, thanks for the followup guys. Will try this now and report back.

 Signature 

System Info: Production

EE Version:  1.67
Build
:       20090320
PHP Version
: 5.2.5
Profile
 
 
Posted: 14 September 2008 11:01 AM   [ Ignore ]   [ # 16 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  534
Joined  12-15-2005

The single quotes seem to work. Why have I never come across this scenario before eh? Thanks muchly all, and I will also make sure to look into the prep conditionals, which I’m not sure I’ve read to date…

 Signature 

System Info: Production

EE Version:  1.67
Build
:       20090320
PHP Version
: 5.2.5
Profile
 
 
Posted: 06 February 2009 09:14 AM   [ Ignore ]   [ # 17 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  877
Joined  02-02-2007

Hi,

I released version 1.5 of Subsegment plugin.

In this release:

1) ditched support for {subsegment} variable and “index” parameter. From this release only variables {subsegment_0}-{subsegment_8} will be supported.

2) added support for conditionals.

Because of the bug conditionals will not work correctly until the build with the fix will be released.

 Signature 

Full list of add-ons here

Child Categories
Browser Sniff
Category Id
Entries Number

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1743, on December 02, 2009 02:47 PM
Total Registered Members: 119463 Total Logged-in Users: 63
Total Topics: 125730 Total Anonymous Users: 32
Total Replies: 661649 Total Guests: 401
Total Posts: 787379    
Members ( View Memberlist )