From ccba9dcd7c0681f1e0b36582dca7b5242ae42ec0 Mon Sep 17 00:00:00 2001 From: Igor Nikolaev Date: Wed, 28 Jul 2021 09:40:34 +0300 Subject: [PATCH] Move templates from Utils lib to make them overridable. --- CHANGELOG.md | 2 + DependencyInjection/DarvinUtilsExtension.php | 23 ++----- Resources/views/data/view/block.html.twig | 63 ++++++++++++++++++++ Resources/views/data/view/table.html.twig | 63 ++++++++++++++++++++ Resources/views/data/view/text.txt.twig | 33 ++++++++++ Resources/views/fields.html.twig | 3 + Resources/views/macros.html.twig | 5 ++ Resources/views/override/entity.php.twig | 41 +++++++++++++ Resources/views/override/repository.php.twig | 19 ++++++ Resources/views/price.html.twig | 5 ++ 10 files changed, 240 insertions(+), 17 deletions(-) create mode 100644 Resources/views/data/view/block.html.twig create mode 100644 Resources/views/data/view/table.html.twig create mode 100644 Resources/views/data/view/text.txt.twig create mode 100644 Resources/views/fields.html.twig create mode 100644 Resources/views/macros.html.twig create mode 100644 Resources/views/override/entity.php.twig create mode 100644 Resources/views/override/repository.php.twig create mode 100644 Resources/views/price.html.twig diff --git a/CHANGELOG.md b/CHANGELOG.md index a252639..1c29641 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -114,3 +114,5 @@ $ /usr/bin/env php bin/console darvin:utils:translations:title-case 7.3.10: Add Varnish cache clear command. 7.4.0: Add data view services. + +7.5.0: Move templates from Utils lib to make them overridable. diff --git a/DependencyInjection/DarvinUtilsExtension.php b/DependencyInjection/DarvinUtilsExtension.php index 40e68de..a4e563e 100644 --- a/DependencyInjection/DarvinUtilsExtension.php +++ b/DependencyInjection/DarvinUtilsExtension.php @@ -127,25 +127,14 @@ public function prepend(ContainerBuilder $container): void 'twig', ]); - if ($container->hasExtension('framework') || $container->hasExtension('twig')) { - $dir = sprintf('%s/../Resources', dirname((new \ReflectionClass(ExtensionConfigurator::class))->getFileName())); - - if ($container->hasExtension('framework')) { - $container->prependExtensionConfig('framework', [ - 'translator' => [ - 'paths' => [ - sprintf('%s/translations', $dir), - ], - ], - ]); - } - if ($container->hasExtension('twig')) { - $container->prependExtensionConfig('twig', [ + if ($container->hasExtension('framework')) { + $container->prependExtensionConfig('framework', [ + 'translator' => [ 'paths' => [ - sprintf('%s/views', $dir) => 'DarvinUtils', + sprintf('%s/translations', sprintf('%s/../Resources', dirname((new \ReflectionClass(ExtensionConfigurator::class))->getFileName()))), ], - ]); - } + ], + ]); } } diff --git a/Resources/views/data/view/block.html.twig b/Resources/views/data/view/block.html.twig new file mode 100644 index 0000000..43506ae --- /dev/null +++ b/Resources/views/data/view/block.html.twig @@ -0,0 +1,63 @@ +{% macro block(view, show_title = true) %} +
+ + {% if show_title and view.hasTitle %} + + {{ view.title }} + + {% endif %} + + + + {% if view.empty %} + — + {% else %} + + {% if view.hasChildren %} + + {% if view.associative %} +
    + + {% for child in view.children %} +
  • {{ _self.block(child) }}
  • + {% endfor %} + +
+ {% else %} +
    + + {% for child in view.children %} +
  1. {{ _self.block(child) }}
  2. + {% endfor %} + +
+ {% endif %} + + {% endif %} + + {% if view.hasValue %} + + {% if view.hasUrl %} + + {{ view.value }} + + {% else %} + {{ view.value|raw }} + {% endif %} + + {% endif %} + + {% endif %} + +
+
+{% endmacro %} + +{% if view %} + + {{ _self.block(view, false) }} + +{% endif %} diff --git a/Resources/views/data/view/table.html.twig b/Resources/views/data/view/table.html.twig new file mode 100644 index 0000000..1b07a36 --- /dev/null +++ b/Resources/views/data/view/table.html.twig @@ -0,0 +1,63 @@ +{% macro row(view, show_title = true) %} + + + {% if show_title and view.hasTitle %} + {{ view.title }} + {% endif %} + + + + {% if view.empty %} + — + {% else %} + + {% if view.hasChildren %} + + + {% for child in view.children %} + + {{ _self.row(child) }} + + {% endfor %} + +
+ {% endif %} + + {% if view.hasValue %} + + {% if view.hasUrl %} + + {{ view.value }} + + {% else %} + {{ view.value|raw }} + {% endif %} + + {% endif %} + + {% endif %} + + + +{% endmacro %} + +{% if view %} + + + {% if view.hasChildren %} + + {% for child in view.children %} + + {{ _self.row(child) }} + + {% endfor %} + + {% else %} + + {{ _self.row(view, false) }} + + {% endif %} + +
+{% endif %} diff --git a/Resources/views/data/view/text.txt.twig b/Resources/views/data/view/text.txt.twig new file mode 100644 index 0000000..3810385 --- /dev/null +++ b/Resources/views/data/view/text.txt.twig @@ -0,0 +1,33 @@ +{%- macro text(view, level = 0) -%} + + {%- if level > 0 and view.hasTitle -%} + {{- view.title ~ ': ' -}} + {%- endif -%} + + {%- if view.empty -%} + - + {%- else -%} + + {%- if view.hasChildren -%} + + {%- for child in view.children %} + +{% if level > 0 %}{% for i in 1..level %}{{ ' ' }}{% endfor %}{% endif %}{{ _self.text(child, level + 1) -}} + + {%- endfor -%} + + {%- endif -%} + + {%- if view.hasValue -%} + {{- view.value|striptags -}} + {%- endif -%} + + {%- endif -%} + +{%- endmacro -%} + +{%- if view -%} + + {{- _self.text(view) -}} + +{%- endif -%} diff --git a/Resources/views/fields.html.twig b/Resources/views/fields.html.twig new file mode 100644 index 0000000..0ec628a --- /dev/null +++ b/Resources/views/fields.html.twig @@ -0,0 +1,3 @@ +{%- block darvin_utils_anti_spam_row -%} + {{- form_widget(form) -}} +{%- endblock darvin_utils_anti_spam_row -%} diff --git a/Resources/views/macros.html.twig b/Resources/views/macros.html.twig new file mode 100644 index 0000000..63c1f4d --- /dev/null +++ b/Resources/views/macros.html.twig @@ -0,0 +1,5 @@ +{% macro div_data(data, name, trans_domain = 'messages') %}{{ utils_data_render('block', data, name, trans_domain) }}{% endmacro %} + +{% macro plain_data(data, name, trans_domain = 'messages') %}{{ utils_data_render('text', data, name, trans_domain) }}{% endmacro %} + +{% macro table_data(data, name, trans_domain = 'messages') %}{{ utils_data_render('table', data, name, trans_domain) }}{% endmacro %} diff --git a/Resources/views/override/entity.php.twig b/Resources/views/override/entity.php.twig new file mode 100644 index 0000000..e984bf5 --- /dev/null +++ b/Resources/views/override/entity.php.twig @@ -0,0 +1,41 @@ + 0 or options.allow_empty %} + {{ ('price.value.' ~ options.format)|trans({'%price%': price|number_format(0, null, options.thousands_separator), '%count%': price|number_format(0, '', '')})|raw }} +{% else %} + {{ ('price.empty.' ~ options.empty_format)|trans|raw }} +{% endif %}