Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Twig Jack Bundle Laravel Package

boekkooi/twig-jack-bundle

View on GitHub
Deep Wiki
Context7

Twig Doctrine Loader

This extension allows you to add one or multiple Twig Doctrine Loaders to you system. By default, this extension is disabled.

Add a loader

# app/config/config.yml
boekkooi_twig_jack:
    ...
    loaders:
        my_loader:
            prefix: 'db://'
            type: orm
            model_class: 'My\Model\Template'

The above configuration will add a custom loader for any template that starts with db://.

To make your loader i18n compatible, set the locale_callable option to a service id in the container. This service must return a callable that when called returns a locale/string.

Example

This example is using knplabs/doctrine-behaviors for the translations.

First we create the required models:

<?php
# My\Model\Template
namespace My\Model;

use Boekkooi\Bundle\TwigJackBundle\Model\TemplateInterface;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * [@ORM](https://github.com/ORM)\Entity
 * [@ORM](https://github.com/ORM)\Table(name="my_template")
 */
class Template implements TemplateInterface
{
    use ORMBehaviors\Translatable\Translatable;

    /**
     * [@ORM](https://github.com/ORM)\Id
     * [@ORM](https://github.com/ORM)\Column(name="id", type="string", length=100)
     */
    protected $identifier;

    /**
     * Constructor
     *
     * [@param](https://github.com/param) string $identifier The template unique identifier
     */
    public function __construct($identifier)
    {
        $this->identifier = $identifier;
    }

    /**
     * {[@inheritdoc](https://github.com/inheritdoc)}
     */
    public function getIdentifier()
    {
        return $this->identifier;
    }

    /**
     * {[@inheritdoc](https://github.com/inheritdoc)}
     */
    public function getTemplate()
    {
        return $this->translate()->getTemplate();
    }

    /**
     * {[@inheritdoc](https://github.com/inheritdoc)}
     */
    public function getLastModified()
    {
        return $this->translate()->getLastModified();
    }
}
<?php
# My\Model\Template
namespace My\Model;

use Boekkooi\Bundle\TwigJackBundle\Model\TranslatableTemplateInterface;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;

/**
 * [@ORM](https://github.com/ORM)\Entity
 * [@ORM](https://github.com/ORM)\Table(name="my_template_translation")
 */
class TemplateTranslation implements TranslatableTemplateInterface
{
    use ORMBehaviors\Timestampable\Timestampable,
        ORMBehaviors\Translatable\Translation;

    /**
     * [@ORM](https://github.com/ORM)\Column(type="text")
     */
    protected $template = '';

    public function __construct()
    {
        $this->updateTimestamps();
    }

    /**
     * {[@inheritdoc](https://github.com/inheritdoc)}
     */
    public function getTemplate()
    {
        return $this->template;
    }

    public function setTemplate($template)
    {
        Assertion::string($template);

        $this->template = $template;
    }

    public function getLastModified()
    {
        return $this->updatedAt ?: $this->createdAt;
    }
}

To add translation support to the templates we need to add a locale callable. In this case we will be using Knp\DoctrineBehaviors\ORM\Translatable\CurrentLocaleCallable, this object implements __invoke to make it callable.

Add the current locale callable to your services:

services:
  my.current_locale_callable:
    class: Knp\DoctrineBehaviors\ORM\Translatable\CurrentLocaleCallable
    arguments:
        - [@service_container](https://github.com/service_container)

Now we update the configuration.

# app/config/config.yml
boekkooi_twig_jack:
    ...
    loaders:
        my_loader:
            prefix: 'db://'
            type: orm
            model_class: 'My\Model\Template'
            locale_callable: 'my.current_locale_callable'

You're done! Let's use it:

# Some controller or mailer or whatever
$this->container->get('templating')->render('db://my_template', array());

Full configuration

# app/config/config.yml
boekkooi_twig_jack:
    ...
    loaders:
        my_loader:
            prefix: 'db://' # A template name prefix
            type: orm # Choose orm, mongo, couch or custom
            repository: 'my_custom_repo' # Only used when type is custom
            model_class: 'My\Model\Template'
            locale_callable: 'current_locale_callable' # A service identifier that when invoked returns the locale to use
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
bugban/symfony
beyonder-capi/workflow-extensions-bundle
beyonder-capi/job-queue-bundle
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin