Jump to content

ProcessString


ukyo
 Share

Recommended Posts

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>&#123;label(title)&#125;</code> <b>RESULT :</b> <code>{label(title)}</code></li>
        <li><b>02: GET FIELD LABEL WITH PREFIX</b> <code>&#123;label(title~=> )&#125;</code> <b>RESULT :</b> <code>{label(title~=> )}</code></li>
        <li><b>03: GET FIELD LABEL WITH SUFFIX</b> <code>&#123;label(title~~ <=)&#125;</code> <b>RESULT :</b> <code>{label(title~~ <=)}</code></li>
        
        <li><b>04: GET FIELD DESCRIPTION</b> <code>&#123;description(title)&#125;</code> <b>RESULT :</b> <code>{description(title)}</code><br>
        <li><b>05: GET FIELD DESCRIPTION WITH PREFIX</b> <code>&#123;description(title~=> )&#125;</code> <b>RESULT :</b> <code>{description(title~=> )}</code></li>
        <li><b>06: GET FIELD DESCRIPTION WITH SUFFIX</b> <code>&#123;description(title~~ <=)&#125;</code> <b>RESULT :</b> <code>{description(title~~ <=)}</code></li>
        
        <li><b>07: GET FIELD NOTES</b> <code>&#123;notes(title)&#125;</code> <b>RESULT :</b> <code>{notes(title)}</code><br>
        <li><b>08: GET FIELD NOTES WITH PREFIX</b> <code>&#123;notes(title~=> )&#125;</code> <b>RESULT :</b> <code>{notes(title~=> )}</code></li>
        <li><b>09: GET FIELD NOTES WITH SUFFIX</b> <code>&#123;notes(title~~ <=)&#125;</code> <b>RESULT :</b> <code>{notes(title~~ <=)}</code></li>
        
        <li><b>10: GET PAGE TITLE</b> <code>&#123;page(title)&#125;</code> <b>RESULT :</b> <code>{page(title)}</code></li>
        <li><b>11: GET PAGE TITLE</b> <code>&#123;page:title&#125;</code> <b>RESULT :</b> <code>{page:title}</code></li>
        <li><b>12: GET PAGE RENDER TITLE</b> <code>&#123;page:render:title&#125;</code> <b>RESULT :</b> <code>{page:render:title}</code></li>
        
        <li><b>12: GET HOMEPAGE TITLE</b> <code>&#123;pages:get(template=home):title&#125;</code> <b>RESULT :</b> <code>{pages:get(template=home):title}</code></li>
        <li><b>13: GET HOMEPAGE TEMPLATE ID</b> <code>&#123;pages:get(template=home):template:id&#125;</code> <b>RESULT :</b> <code>{pages:get(template=home):template:id}</code></li>
    </ul>
    ";
echo processString($str);
?>
  • Like 2
Link to comment
Share on other sites

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.

 

 

  • Like 2
Link to comment
Share on other sites

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 :).

59e4df334426c_EkranResmi2017-10-1619_32_20.png.ac199843ab6c47f4337bb03edcbecf1f.png

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.

59e4dffd2dce8_EkranResmi2017-10-1619_34_34.png.f93e549d5b65a653ce4a20a3e78d556c.png

I will use grid-list component as example. Here is index.php for configs.

59e4e0890d9b6_EkranResmi2017-10-1619_37_09.thumb.png.e678819ad748bfc4dd8d5a41afae2017.png

And here is template.php file for display.

59e4e15f15884_EkranResmi2017-10-1619_41_34.thumb.png.65af25a167b3b2450c94506212522d70.png

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 :

59e4e2d725125_EkranResmi2017-10-1619_47_58.thumb.png.ec2252ffb32a95a5b2668ea8688de0e3.png

Same component call on another template :

<?= co::component($finder['items'], 'grid-list') ?>

Result :

59e4e34004ae5_EkranResmi2017-10-1619_49_27.thumb.png.39a8547d32da4019c0c02b6902be3907.png

 

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.

Link to comment
Share on other sites

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

 

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

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: 

5a3794e9f3045_ScreenShot2017-12-18at11_13_44.thumb.png.f84427109f604e38c75c0ddaf7c75246.png

 

@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();

 

Link to comment
Share on other sites

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 :)

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

×
×
  • Create New...