Textpattern Forum

You are not logged in. Register | Login | Help

#1 2012-06-03 15:22:10

etc
Plugin Author
Registered: 2010-11-11
Posts: 1,062
Website

etc_url: rewrite some parts of url

This tiny plugin will take an url and append (or rewrite) some query string to it, like this:

<txp:etc_url url="http://my.web.site/section/tag/?q=etc&pg=2" query="pg=3" />

will produce http://my.web.site/index.php?s=tag&q=etc&pg=3 in messy mode, and http://my.web.site/tag/?q=etc&pg=3 in /section/title permalink mode.

Semantic txp parameters (like section or category) do receive no special treatment in clean mode, i.e. any only ?query part of url is parsed.

Attributes

  • url: url to rewrite, default is the current page url.
  • query: &-separated list of tokens to append/modify, like this: key1=val1&key2&key3=val3. If val is not set, the key will be removed from url.
  • sanitize: comma-separated URL parameters that should be transformed following the same rules as for article titles.
  • type: URL parameter to extract, default none (the whole URL). If type starts with &, it’s value will be URL-encoded.
  • custom: whether url should be parsed.

History

Version 0.1: initial release.
Version 0.2: respects all permalink modes. Added custom attribute.
Version 0.3: Added sanitize and type attributes, removed custom attribute.

Last edited by etc (2013-05-24 20:21:39)

Offline

#2 2012-06-03 20:59:05

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 547
Website

Re: etc_url: rewrite some parts of url

Very good of you to release this, etc. I will look into the pagination method. I still need to understand xpath, but it is something I want to do sooner than later. :-)

Offline

#3 2012-06-04 13:56:39

etc
Plugin Author
Registered: 2010-11-11
Posts: 1,062
Website

Re: etc_url: rewrite some parts of url

maruchan wrote:

Very good of you to release this, etc.

Thank you for your welcome, and have fun with xpath! It is just another language for DOM traversing, with very similar to jQuery or CSS syntax, that is understood by php (and now by TXP:-) too.

Offline

#4 2012-06-12 09:12:44

etc
Plugin Author
Registered: 2010-11-11
Posts: 1,062
Website

Re: etc_url: rewrite some parts of url

Version 0.11 : added queue attribute, see this tip for usage example. Removed

Last edited by etc (2013-04-09 09:17:58)

Offline

#5 2013-04-09 09:19:42

etc
Plugin Author
Registered: 2010-11-11
Posts: 1,062
Website

Re: etc_url: rewrite some parts of url

Version 0.2: respects permalink modes.

Edit: it can also create links to articles filtered by multiple criteria that will fit any permalink mode:

<txp:etc_url url="" query="c=novels&author=Jack London&month=1903" />

Last edited by etc (2013-04-09 12:16:24)

Offline

#6 2013-05-21 10:04:35

kees-b
Member
From: middelburg, nl
Registered: 2004-03-03
Posts: 198
Website

Re: etc_url: rewrite some parts of url

Hi, can I use etc_url for constructing an url from the content of a custom field?

Example:

In the custom field: two words
The url I want: http://sitename.com/section/two-words The section is not the same as the article itself, no need to generate it dynamicly for this use.
The url rewriting should follow the same rules as textpattern uses to generate an url from an article title. This means: words need to be stripped from diacritics, capitals removed, connected by a dash.

Another more sophisticated approach might be:

1: read the contents of the custom field
2: look for an article with the same title
3: if it exists: output the url to use it as a link

But I probably need another plugin for that.

-kees

Offline

#7 2013-05-21 10:48:22

etc
Plugin Author
Registered: 2010-11-11
Posts: 1,062
Website

Re: etc_url: rewrite some parts of url

Hi,

I don’t think you need a plugin for this. If your custom field is called, say, link, try

<txp:variable name="link"><txp:php>
	global $thisarticle;
	echo stripSpace($thisarticle['link'], 1);
</txp:php></txp:variable>

<a href='/section/<txp:variable name="link" />'><txp:custom_field name="link" /></a>
<!-- will generate something like <a href='/section/two-words'>Two Words</a> -->

Use etc_url only if you need to respect the site url mode:

<txp:etc_url url="" query='s=<txp:variable name="link" />' />
<!-- will generate something like <a href='http://sitename.com/?s=two-words'>Two Words</a> in messy mode -->

Offline

#8 2013-05-21 11:05:51

kees-b
Member
From: middelburg, nl
Registered: 2004-03-03
Posts: 198
Website

Re: etc_url: rewrite some parts of url

etc wrote:

Hi,

I don’t think you need a plugin for this. If your custom field is called, say, link, try

<txp:variable name="link"><txp:php>
	global $thisarticle;
	echo stripSpace($thisarticle['link'], 1);
</txp:php></txp:variable>

<a href='/section/<txp:variable name="link" />'><txp:custom_field name="link" /></a>
<!-- will generate something like <a href='/section/two-words'>Two Words</a> -->

Thanks! I allready thought this should be possible with txp:variable. I’ll give it a try.

-k

Offline

#9 2013-05-22 19:31:31

kees-b
Member
From: middelburg, nl
Registered: 2004-03-03
Posts: 198
Website

Re: etc_url: rewrite some parts of url

you’re suggested solution with txp:variable does the trick perfectly!

thanks!

-kees

Offline

Board footer

Powered by FluxBB