With this package you can manage & generate strings with tokens (shortcodes), it seems like CMS Drupal.
Run from the command line:
composer require fomvasss/laravel-str-tokens
To publish the configs, run the following command:
php artisan vendor:publish --provider="Fomvasss\LaravelStrTokens\ServiceProvider"
Configuration file will be publish to config/str-tokens.php
The configuration fill will allow you to control how tokens are parsed using token_match_pattern
and token_split_character
You can decide if a token can traverse eloquent model relationships using can_traverse_relations
You can globally limit what model fields are allowed as tokens using disable_model_tokens
You can also limit what tokens are exposed via individual models by creating a strTokenWhitelist
or strTokenBlacklist
function that returns an array of valid patterns
// Use Eloquent model, vars and date for generate tokens
$str = StrToken::setText('
Example str with tokens for article: "[article:title]([article:id])",
Article created at date: [article:created_at],
Author: [article:user:name]([article:user:id]).
Article first category: [article:txArticleCategories:name],
Article root category: [article:txArticleCategories:root:name],
Article status: [article:txArticleStatus:name],
User: [article:user:email], [article:user:city:country:title], [article:user:city:title].
Generated token at: [config:app.name], [date:raw]
[article:test:Hello]!!!
Length: [var:length];
Width: [var:width];
Price: [var:price]
')
->setDate(\Carbon\Carbon::tomorrow())
->setEntity(\App\Model\Article::findOrFail(13))
->setVars(['length' => '2.2 m.', 'width' => '3.35 m.'])
->setVar('price', '$13')
->replace();
// Print result text
print_r($str);
/*
Example str with tokens for article: "Test article title(23)",
Article created at date: 15.07.2018,
Author: Taylor Otwell(1),
Article first category: Web-programming,
Article root category: Programming,
Article status: article_publish,
User: taylorotwell@gmail.com, AR, Little Rock.
Generated token at: Laravel, 2018-10-27 00:00:00
TEST TOKEN:Hello!!!
Length: 2.2 m.;
Width: 3.35 m.;
Price: $13
*/
You can use method setEntities()
for set many Eloquent models, for example:
<?php
$user1 = User::find(1);
$user2 = User::find(2);
$article = Article::first();
$str = StrToken::setText('
User1: [user1:name] / [user1:email]
User2: [user2:name] / [user2:email]
Article: "[firstArticle:title]"
')->setEntities([
'user1' => $user1,
'user2' => $user2,
'firstArticle' => $article,
])->replace();
/*
User: Taylor Otwell / taylorotwell@gmail.com
User: Vasyl Fomin / fomvasss@gmail.com
Article: "Laravel is awesome framework"
*/
In your models you can create own methods for generate tokens.
The names of these methods must begin with strToken
.
In next example, we create custom methods: strTokenTest()
, strTokenCreatedAt()
And now we can use next token in string:
This is [article:test], created at: [article:creted_at]
And result:
This is "TEST TOKEN", created at: 23.11.2018
Example Article
Eloquent model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Fomvasss\Taxonomy\Models\Traits\HasTaxonomies;
class Article extends Model
{
use HasTaxonomies;
//...
public function strTokenTest($entity, $method, $attr): string
{
// $entity - this article
// $method - "test"
// $attr - additional args
return 'TEST TOKEN:' . $attr;
}
public function strTokenCreatedAt(): string
{
return $this->created_at->format('d.m.Y');
}
// For package https://github.com/fomvasss/laravel-taxonomy
public function txArticleCategories()
{
return $this->termsByVocabulary('product_categories');
}
// For package https://github.com/fomvasss/laravel-taxonomy
public function txArticleStatus()
{
return $this->term('status', 'system_name')
->where('vocabulary', 'post_statuses');
}
}
Example Term
model:
<?php
namespace App\Models\Taxonomies;
use App\Article;
class Term extends \Fomvasss\Taxonomy\Models\Term
{
public function articles()
{
return $this->morphedByMany(Article::class, 'termable');
}
/**
* Method for generate next example token for article model:
* [article:txArticleCategories:root:name]
*
* @param $entity
* @param $r
* @param $param
* @return mixed
*/
public function strTokenRoot($entity, $r, $param)
{
if ($root = $entity->ancestors->first()) {
return $root->{$param};
}
return $entity->{$param};
}
}
@php(\StrToken::setEntity($article)->setDate($article->created_at))
@php(\StrToken::setText('[article:title] - [date:short]'))
<h3>{!! \StrToken::replace() !!}</h3>
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.