ukyo Posted October 16, 2017 Posted October 16, 2017 Call ProcessWire API Functions Inside STRING Github Repo Current status : BETA Each called method must return string value ! I added all functions, but not tested all. I focused page(), page()->render and field properties (label, description and notes). I also tested some basic pages() api calls. Your API calls must start with { and must end with }. For use multiple arguments inside functions, separate arguments with ~ char. NOTE If you pass directly arguments to api {page(title)}, this call will check for requestedApiCall()->get(arguments). USAGE processString(string, page, language); Get page title <?php $str = "You are here : {page:title}"; echo processString($str); ?> Get page children render result <?php $str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.<hr>{page:render:children}"; echo processString($str); ?> Get homepage title <?php $str = "You can visit our <a hre='{pages(1):url}'>{pages:get(1):title}</a>"; echo processString($str); ?> Get title field label, description or notes <?php $str = "Our title field properties are : label: {label(title)} - description: {description(title)} - notes: {notes(title)}"; echo processString($str); ?> Multiple examples <?php $str = " <ul class='uk-list uk-list-striped'> <li><b>01: GET FIELD LABEL</b> <code>{label(title)}</code> <b>RESULT :</b> <code>{label(title)}</code></li> <li><b>02: GET FIELD LABEL WITH PREFIX</b> <code>{label(title~=> )}</code> <b>RESULT :</b> <code>{label(title~=> )}</code></li> <li><b>03: GET FIELD LABEL WITH SUFFIX</b> <code>{label(title~~ <=)}</code> <b>RESULT :</b> <code>{label(title~~ <=)}</code></li> <li><b>04: GET FIELD DESCRIPTION</b> <code>{description(title)}</code> <b>RESULT :</b> <code>{description(title)}</code><br> <li><b>05: GET FIELD DESCRIPTION WITH PREFIX</b> <code>{description(title~=> )}</code> <b>RESULT :</b> <code>{description(title~=> )}</code></li> <li><b>06: GET FIELD DESCRIPTION WITH SUFFIX</b> <code>{description(title~~ <=)}</code> <b>RESULT :</b> <code>{description(title~~ <=)}</code></li> <li><b>07: GET FIELD NOTES</b> <code>{notes(title)}</code> <b>RESULT :</b> <code>{notes(title)}</code><br> <li><b>08: GET FIELD NOTES WITH PREFIX</b> <code>{notes(title~=> )}</code> <b>RESULT :</b> <code>{notes(title~=> )}</code></li> <li><b>09: GET FIELD NOTES WITH SUFFIX</b> <code>{notes(title~~ <=)}</code> <b>RESULT :</b> <code>{notes(title~~ <=)}</code></li> <li><b>10: GET PAGE TITLE</b> <code>{page(title)}</code> <b>RESULT :</b> <code>{page(title)}</code></li> <li><b>11: GET PAGE TITLE</b> <code>{page:title}</code> <b>RESULT :</b> <code>{page:title}</code></li> <li><b>12: GET PAGE RENDER TITLE</b> <code>{page:render:title}</code> <b>RESULT :</b> <code>{page:render:title}</code></li> <li><b>12: GET HOMEPAGE TITLE</b> <code>{pages:get(template=home):title}</code> <b>RESULT :</b> <code>{pages:get(template=home):title}</code></li> <li><b>13: GET HOMEPAGE TEMPLATE ID</b> <code>{pages:get(template=home):template:id}</code> <b>RESULT :</b> <code>{pages:get(template=home):template:id}</code></li> </ul> "; echo processString($str); ?> 2
adrian Posted October 16, 2017 Posted October 16, 2017 Hi @ukyo - thanks for sharing this, but I must admit I am little confused. I am honestly not really sure what you are trying to achieve - no offense intended - I am probably just missing something here Can you please explain how: <?php $str = "You can visit our <a hre='{pages(1):url}'>{pages:get(1):title}</a>"; echo processString($str); ?> is any better than using the API variables in a string like this: <?php $str = "You can visit our <a hre='{$pages(1)->url}'>{$pages->get(1)->title}</a>"; echo $str; ?> BTW, pages(1) and pages->get(1) are the same thing. The other thing you might want to take a look at is the tag compiler: https://processwire.com/blog/posts/processwire-3.0-alpha-2-and-2.6.22-rc1/#new-module-file-compiler-tags which is not really what you are doing, but it's sort of related. 2
ukyo Posted October 16, 2017 Author Posted October 16, 2017 I missed : https://processwire.com/blog/posts/processwire-3.0-alpha-2-and-2.6.22-rc1/#new-module-file-compiler-tags I will check it. @adrian For directly working on templates you are right. I am using same method with you. I wrote this functional group for own my needs and want to share it with community. I am working on a module (Component system for uikit-2 and uikit-3). At this component system, you can change lots of things by configs. Let me explain you little, I hope you can understand me :). Each component have index.php for component configs and template.php file for view. Also you can extend, overwrite or add new component for site specific. I will use grid-list component as example. Here is index.php for configs. And here is template.php file for display. As you see above selected area, i am using processString() function for custom title and subtitle configs. Component call on template : <?= co::component($finder['items'], 'grid-list', [ 'subtitle' => '{page:length} {notes(length)} // {page:cabins} {notes(cabins)} // {page:render:destinations}' ]); ?> Result : Same component call on another template : <?= co::component($finder['items'], 'grid-list') ?> Result : For my component module, I can have 1 component and i can use it on all my sites with different settings and different display options. <?= // 1 more example with "alert" component co::c('alert', [ 'title' => 'You are in {page('title')}.', 'content' => 'You need to go <a href="{pages(1200):url}">{pages(1200):title}</a> for view our products', 'alert_style' => 'danger', 'alert_size' => true, 'alert_close' => true, 'text_align' => 'center' ]); ?> If you want you can call something inside body field.
ukyo Posted October 16, 2017 Author Posted October 16, 2017 I checked : https://processwire.com/blog/posts/processwire-3.0-alpha-2-and-2.6.22-rc1/#new-module-file-compiler-tags, Not allowing function calls, whats happening inside loop ? If i want to call item based tags. Its not meet my needs {pages(template=template-name):title} or {label(title)} or {description(title)} or {notes(title)}
tpr Posted October 16, 2017 Posted October 16, 2017 I recommend looking into template engines instead, eg. my Latte module uses a similar {$page->field} syntax. There's a lot of additional filters/macros I've made in the last 1-2 years that allows quickly building up a site (getting config strings from a textarea field, generating srcset/breadcrumb/paginator markup, truncate strings/html, add active class, etc). Plus you can use php functions also if you need. But of course other template engines could do the job too. https://github.com/rolandtoth/TemplateLatteReplace/wiki 1
ukyo Posted October 17, 2017 Author Posted October 17, 2017 @tpr I don't want to use a TemplateEngine. My module meet my needs and i am ok. Thanks for your advice !
Noel Boss Posted December 18, 2017 Posted December 18, 2017 Thank you ukyo I've tried your module and it works like a charm out of the box with my setup. Installation via Module Directory and Class Name failed tough: @adrian I use this module to pare content of field in a Mail.php template that I use to send mails - I want the editor to be able to translate and edit the contents of this mail – how would you suggest doing it? I'm sending mails using custom code trough Mandrill and pulling it's content from a field like so: You are invited by {user:fullname} to join {tem}! <a href='{invite:httpUrl}'>Click here to accept the invitation!</a> $template = $pages->get('/mails/invite/'); $subject = processString($template->header); $message = processString($template->render()); $mail = wire('modules')->get('WireMailMandrill'); $mail->to($input->whitelist('email'), $invite->firstname.' '.$invite->lastname); $emailFrom = wire('config')->adminEmail; $mail->from($emailFrom, 'Sender'); $mail->subject($subject); $mail->bodyHTML($message); $count = $mail->___send();
adrian Posted December 18, 2017 Posted December 18, 2017 3 hours ago, noelboss said: how would you suggest doing it? Are you asking how I would do it without this module because of my question above about why this module's approach is better than calling the functions directly? I would have just written the code to parse those tags myself or I would simply the tags that are required by the editor. Maybe simply {fullname} and {invitelink} and do a simple: $subject = str_replace('{invitelink}, $invite->link, $subject); Or if it works better for you, then I think it's a great use case for this module
Noel Boss Posted December 19, 2017 Posted December 19, 2017 Thanks at @adrian – I've also found TextformatterTagParser which also meets my needs… 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now