Skip to main content
added 5 characters in body
Source Link
Dom Stubbs
  • 548
  • 4
  • 8

Here’s a Craft 3-friendly way of achieving what Mike suggested. This assumes your app already has a custom twig extension, if it doesn’t you can easily create one.

Add use craft\web\View; to the top of your TwigExtension, then in getFunctions() add your function definition:

new \Twig\TwigFunction('field', [$this, 'renderFormMacro'], ['is_safe' => ['html']]),

Then the function itself:

public function renderFormMacro(string $fieldType, array $fieldOptions) : string
{
    $oldMode = \Craft::$app->view->getTemplateMode();
    \Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);
    $html = \Craft::$app->view->renderTemplate>renderTemplateMacro('_includes/forms/' .forms', $fieldType, $fieldOptions[$fieldOptions]);
    \Craft::$app->view->setTemplateMode($oldMode);

    return $html;
}

You can then display fields in your frontend templates with code like this:

{{ field('text', {
    name: 'location',
    value: 'Timbuktu'
}) }}

Here’s a Craft 3-friendly way of achieving what Mike suggested. This assumes your app already has a custom twig extension, if it doesn’t you can easily create one.

Add use craft\web\View; to the top of your TwigExtension, then in getFunctions() add your function definition:

new \Twig\TwigFunction('field', [$this, 'renderFormMacro'], ['is_safe' => ['html']]),

Then the function itself:

public function renderFormMacro(string $fieldType, array $fieldOptions) : string
{
    $oldMode = \Craft::$app->view->getTemplateMode();
    \Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);
    $html = \Craft::$app->view->renderTemplate('_includes/forms/' . $fieldType, $fieldOptions);
    \Craft::$app->view->setTemplateMode($oldMode);

    return $html;
}

You can then display fields in your frontend templates with code like this:

{{ field('text', {
    name: 'location',
    value: 'Timbuktu'
}) }}

Here’s a Craft 3-friendly way of achieving what Mike suggested. This assumes your app already has a custom twig extension, if it doesn’t you can easily create one.

Add use craft\web\View; to the top of your TwigExtension, then in getFunctions() add your function definition:

new \Twig\TwigFunction('field', [$this, 'renderFormMacro'], ['is_safe' => ['html']]),

Then the function itself:

public function renderFormMacro(string $fieldType, array $fieldOptions) : string
{
    $oldMode = \Craft::$app->view->getTemplateMode();
    \Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);
    $html = \Craft::$app->view->renderTemplateMacro('_includes/forms', $fieldType, [$fieldOptions]);
    \Craft::$app->view->setTemplateMode($oldMode);

    return $html;
}

You can then display fields in your frontend templates with code like this:

{{ field('text', {
    name: 'location',
    value: 'Timbuktu'
}) }}
Source Link
Dom Stubbs
  • 548
  • 4
  • 8

Here’s a Craft 3-friendly way of achieving what Mike suggested. This assumes your app already has a custom twig extension, if it doesn’t you can easily create one.

Add use craft\web\View; to the top of your TwigExtension, then in getFunctions() add your function definition:

new \Twig\TwigFunction('field', [$this, 'renderFormMacro'], ['is_safe' => ['html']]),

Then the function itself:

public function renderFormMacro(string $fieldType, array $fieldOptions) : string
{
    $oldMode = \Craft::$app->view->getTemplateMode();
    \Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP);
    $html = \Craft::$app->view->renderTemplate('_includes/forms/' . $fieldType, $fieldOptions);
    \Craft::$app->view->setTemplateMode($oldMode);

    return $html;
}

You can then display fields in your frontend templates with code like this:

{{ field('text', {
    name: 'location',
    value: 'Timbuktu'
}) }}