Jump to content

Using PW variables in descriptions and notes


adrian
 Share

Recommended Posts

Would anyone else like to see the ability to make use of PW variables to build up dynamic descriptions and notes for fields?

This is a very simplistic example and not that useful, but I do have some use cases where the ability to do something like this would be very handy:

$f->description = __('Please make sure you fill out this field based on the content of the {page.parent.title} summary field.');

As I said - a bad example, but I think this could be a powerful addition in some scenarios.

  • Like 6
Link to comment
Share on other sites

sometimes i wished the descriptions support html for linking to something. if it supported html+php we could combine both:

eg ...based on the content of <a href="<?php echo $page->parent->url; ?>"><?php echo $page->parent->title; ?></a>'s summary field...

but i have absolutely no idea how much effort/overhead that would be :)

  • Like 1
Link to comment
Share on other sites

You can link by using markdown [my-link](http://processwire.com). But be aware, it's not a full-blown markdown parser, which does this job, so other syntax might or might not work. 

About the overhead. Ryan did mention somewhere, when someone asked about better markdown support, that that would be a performance hit, so I'm not sure if it's wise to back notes/descriptions with features. It's really one of those "called everywhere and often" things.

  • Like 2
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

Thanks Bernhard - your module looks great. I also put together something for this task myself. I just added it to my gists:

https://gist.github.com/adrianbj/5dc1c00f1617b2639319

It allows you to do something like this in a field's description or notes text: 

[Click Here]({page.parent.url})

This will give you a "Click Here" link to the parent page. Of course you can also use "name", "title", and any other field from the current page, or it's parent, or parent above that.

I haven't needed it yet, but I was thinking it might be nice to be able to do {page[xxxx].parent.title} so you can specify the ID of the page you want to reference. Maybe at some point I'll add that, or if anyone out there would make use of it, let me know and I'll add it sooner.

While not as flexible as your module, it requires no setup so it really depends on what you need.

  • Like 3
Link to comment
Share on other sites

Just made some changes to the gist.

  • It now uses square brackets [ ] and not curly braces {}
     
  • It now limits replacements to just description and notes paragraphs
     
  • You can now define str_replace $search and $replace like this. In this example I needed the dashes in $page->name replaced with underscores, but you can use however you want.
[page.name.(-|_)]

Not sure that my code/regexes are the cleanest - I needed this is a bit of a rush, but it seems to work well and I am really finding this very useful in many of my fields. I'd definitely be keen on hearing feedback from anyone else who tries it and if you think it warrants being included in the modules directory. I think I will likely use it on most new sites, but maybe it's just me :)

Link to comment
Share on other sites

Ryan should consider this to be included in the core I think.

I completely agree - I think it would be more targeted and efficient that way, although I am guessing he probably won't want to bother with the str_replace option I just introduced. My need for that is probably an edge case though :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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