Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

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

etc
Developer
Registered: 2010-11-11
Posts: 4,252
Website GitHub

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.
  • context: article, file, image or link. To be used in an article (file, …) form for custom schemes compatibility and/or faster processing. Empty by default.
  • 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.
  • mode: optional TXP permlink mode used to parse url.

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.
Version 0.3.4: added context and mode attributes.

Last edited by etc (2015-09-28 11:03:13)

Offline

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

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 585
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
Developer
Registered: 2010-11-11
Posts: 4,252
Website GitHub

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
Developer
Registered: 2010-11-11
Posts: 4,252
Website GitHub

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
Developer
Registered: 2010-11-11
Posts: 4,252
Website GitHub

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: 235
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
Developer
Registered: 2010-11-11
Posts: 4,252
Website GitHub

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: 235
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: 235
Website

Re: etc_url: rewrite some parts of url

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

thanks!

-kees

Offline

#10 2015-09-24 11:11:13

Gallex
Member
Registered: 2006-10-08
Posts: 1,202

Re: etc_url: rewrite some parts of url

all worked well until owner moved site to another host. now i’m getting this error:

Tag error: <txp:output_form form='main_menu_<txp:variable name="lang" />' /> -> Textpattern Notice: Form not found: main_menu_ while parsing form None on page default_eng

i can’t find what’s wrong. could anbybody take a look? i’ll make a temporary account for you

Offline

#11 2015-09-24 11:56:06

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,238

Re: etc_url: rewrite some parts of url

How is variable lang defined? What might be inside it the moment it’s getting parsed?
And what has the error to do with etc_url?


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#12 2015-09-24 12:22:16

Gallex
Member
Registered: 2006-10-08
Posts: 1,202

Re: etc_url: rewrite some parts of url

uli wrote #295104:

How is variable lang defined? What might be inside it the moment it’s getting parsed?
And what has the error to do with etc_url?

i have multilingual site. i use this plugin to “reflect” some english sections and articles under other languages.

my code:

<txp:variable name="lang" value='<txp:etc_url type="lang" />' />
<txp:variable name="main_menu"><txp:output_form form='main_menu_<txp:variable name="lang" />' /></txp:variable>

<txp:if_variable name="main_menu" value="">
<txp:output_form form="main_menu_eng" />
<txp:else />
<txp:variable name="main_menu" />
</txp:if_variable>

Moderator’s annotation:
Changed bq. to bc. to know curly quotes from straight ones.
– Uli –

Last edited by uli (2015-09-24 12:38:35)

Offline

Board footer

Powered by FluxBB