PHP tag pre-processor [Language]

I am trying to figure out, what PHP tag pre-processor would be used to handle something like this:

{icon}eating {object} at {place-icon}{place-name} with {name1} and {n-more-recipients}.

Or,

{name} shared a link.

I know, that one could use an array[], with preg_replace and replace matching {tag} with a value through a foreach-loop.

What would be an efficient way to implement something like this? Can this be done in OOP?
Should those texts be stored in a txt file of some kind and then read to an array?

I appreciate tips, suggestions, and links to good articles.

I think the term you’re looking for is “template engine”.

I don’t know much about PHP, but Twig is popular.

1 Like

Wrong. I already have a Template engine that does its job. This is different. Think again. Please re-read my question.

Your post doesn’t say you have a template engine and isn’t very clear either.

3 Likes

Now you know that I have a template engine. With that said, I am out for LANGUAGE support. That being said. I am curious, if let’s say: Activity Verb Definitions are to be defined in a TXT file and read into an array along with the tags that are replaced.

I read the question fine.

@gandalf458 and @mawburn, see this example:

{name} shared a link.

Mawburn shared a link.

Mawburn = actor
Shared = verb
link = target, item


From the screenshot, matz marged pull request mruby/mruby#2136

matz = actor
marged = verb
mruby/mruby#2136 = target, item

Do you folks get it now?

Personally, I’m more confused now than before. And I’m pretty sure no one is appreciating your attitude.

So… are you trying to make placeholders such as {name} that you insert values into? Or are you trying to parse the grammar of a sentence to extract the verbs and nouns?

1 Like

Please see the screenshot.

O… K…

Based on the screenshot, my guess is you want to make placeholders such as {name} that you insert values into. In which case a template engine is indeed what you want.

1 Like

From the screenshot, matz marged pull request mruby/mruby#2136

matz = actor
marged = verb
mruby/mruby#2136 = target, item

This is a defined string in English, {name} merged pull request {=request}.

{name} = replaced with matz,
{=request} = replaced with mruby/mruby#2136

And the result,

matz marged pull request mruby/mruby#2136

as in the screenshot, see the last one on the list in the activity stream.

What template engine are you using, currently?

I still have the same question I did in post #8.

Are you trying to use values such as “matz” and “merged” to produce a final string such as “matz merged pull request”?

Or are you trying to use a final string such as “matz merged pull request” to extract specific values such as “matz” and “merged”?

I have one I’ve built from the ground up, in PHP. It works well. However, this is something else, this has to do with STRING definitions in different languages. See my last reply, it includes examples. Thx.

No. Let’s say, I have this line: {name} merged pull request {=request}. defined and saved in a TXT file, so based on user’s language preferences, I read the file that includes string text definitions into an array, then based on user activities, I generate a feed, where I replace e.g. {name} with the name of a user that did something to an object.

It wasn’t a yes or no question.

This explanation is clear and makes sense. I get it now. Thank you.

And the answer is…

…drumroll…

A template engine.

3 Likes

I still don’t see how. To be more exact, to me a template engine is one used to render the whole site. Maybe my definition is poor. I am still curious to know more about TEXT string pre-definitions in files.


Explain to me, how, texts like this would be defined within a traditional HTML template engine.

I think so. A template engine easily renders the view of a website, which is what you’re trying to do here.

Here is a copy and paste from the front page of Twig that I linked in my first reply.

{% for user in users %}
    * {{ user.name }}
{% else %}
    No users have been found.
{% endfor %}

Here is a more complete example I found elsewhere in their site:

<!DOCTYPE html>
<html>
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <ul id="navigation">
        {% for item in navigation %}
            <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
        {% endfor %}
        </ul>

        <h1>My Webpage</h1>
        {{ a_variable }}
    </body>
</html>

A template engine can also handle small snippets just as easily as whole pages.

Here’s what your template with Twig might look like.

$loader = new Twig_Loader_Array([
    'git_log' => '{{name}} merged pull request {{request}}'
]);

$twig = new Twig_Environment($loader);

echo $twig->render('git_log', ['name' => 'matz', 'request' => 'mruby/mruby#2136']);
2 Likes

Hahahahaha! Still I am not convinced that a traditional Template Engine would be used for this kind of job. Picture this, support multilanguage UI as well.

English: {user1} started following {user2}.
German: {user1} angefangen zu folgen {user2}.

If anyone asks, no I am not German.