{"id":139022,"date":"2021-04-08T06:49:33","date_gmt":"2021-04-08T06:49:33","guid":{"rendered":"https:\/\/wordpress.org\/plugins\/a2-twig-templates-for-custom-fields\/"},"modified":"2021-06-04T15:31:36","modified_gmt":"2021-06-04T15:31:36","slug":"np-tiwg","status":"publish","type":"plugin","link":"https:\/\/scn.wordpress.org\/plugins\/np-tiwg\/","author":17128283,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_crdt_document":"","version":"1.0.5","stable_tag":"1.0.5","tested":"5.7.15","requires":"5.0","requires_php":"7.0","requires_plugins":"","header_name":"NP Twig: Front End for Custom Field & ACF","header_author":"NewPast","header_description":"Twig templates for WordPress Custom Fields","assets_banners_color":"","last_updated":"2021-06-04 15:31:36","external_support_url":"","external_repository_url":"","donate_link":"https:\/\/www.buymeacoffee.com\/NewPast","header_plugin_uri":"https:\/\/www.miniindustry.com\/d\/np-twig","header_author_uri":"https:\/\/www.miniindustry.com\/d\/","rating":0,"author_block_rating":0,"active_installs":0,"downloads":7883,"num_ratings":0,"support_threads":0,"support_threads_resolved":0,"author_block_count":0,"sections":["description","installation","faq","changelog"],"tags":{"1.0":{"tag":"1.0","author":"miniindustry","date":"2021-04-08 06:49:28"},"1.0.1":{"tag":"1.0.1","author":"miniindustry","date":"2021-04-08 09:19:03"},"1.0.2":{"tag":"1.0.2","author":"miniindustry","date":"2021-04-11 13:46:15"},"1.0.3":{"tag":"1.0.3","author":"miniindustry","date":"2021-04-16 15:19:16"},"1.0.4":{"tag":"1.0.4","author":"miniindustry","date":"2021-05-28 12:45:50"},"1.0.5":{"tag":"1.0.5","author":"miniindustry","date":"2021-06-04 15:31:36"}},"upgrade_notice":{"":"<p>No special requirements for the upgrade.<\/p>"},"ratings":[],"assets_icons":[],"assets_banners":[],"assets_blueprints":{},"all_blocks":[],"tagged_versions":["1.0","1.0.1","1.0.2","1.0.3","1.0.4","1.0.5"],"block_files":[],"assets_screenshots":{"screenshot-1.png":{"filename":"screenshot-1.png","revision":2516414,"resolution":"1","location":"assets","locale":""},"screenshot-2.png":{"filename":"screenshot-2.png","revision":2516414,"resolution":"2","location":"assets","locale":""}},"screenshots":{"1":"A screenshot of the template edit using WordPress classic editor.","2":"A screenshot of the post NP Twig setting"},"jetpack_post_was_ever_published":false},"plugin_section":[],"plugin_tags":[2211,2010,1578,198216,17809],"plugin_category":[],"plugin_contributors":[198206],"plugin_business_model":[],"class_list":["post-139022","plugin","type-plugin","status-publish","hentry","plugin_tags-acf","plugin_tags-custom-fields","plugin_tags-display","plugin_tags-fron-end","plugin_tags-timber","plugin_contributors-miniindustry","plugin_committers-miniindustry"],"banners":[],"icons":{"svg":false,"icon":"https:\/\/s.w.org\/plugins\/geopattern-icon\/np-tiwg.svg","icon_2x":false,"generated":true},"screenshots":[{"src":"https:\/\/ps.w.org\/np-tiwg\/assets\/screenshot-1.png?rev=2516414","caption":"A screenshot of the template edit using WordPress classic editor."},{"src":"https:\/\/ps.w.org\/np-tiwg\/assets\/screenshot-2.png?rev=2516414","caption":"A screenshot of the post NP Twig setting"}],"raw_content":"<!--section=description-->\n<ul>\n<li>I use <a href=\"https:\/\/twig.symfony.com\/\">Twig<\/a> template to display your post or custom post or <a href=\"https:\/\/wordpress.org\/plugins\/advanced-custom-fields\/\">Advanced Custom Fields<\/a><\/li>\n<li><a href=\"https:\/\/twig.symfony.com\/\">Twig<\/a> and <a href=\"https:\/\/wordpress.org\/plugins\/advanced-custom-fields\/\">ACF<\/a> are a third-party plugin, we used them to render templates<\/li>\n<\/ul>\n\n<h4>Arabic Version of this Article<\/h4>\n\n<ul>\n<li><a href=\"https:\/\/www.miniindustry.com\/d\/ar-np-twig\">https:\/\/www.miniindustry.com\/d\/ar-np-twig<\/a><\/li>\n<\/ul>\n\n<h4>Template requirements<\/h4>\n\n<ul>\n<li>Ensure installs of Timber plugin: <a href=\"https:\/\/wordpress.org\/plugins\/timber-library\/\">https:\/\/wordpress.org\/plugins\/timber-library\/<\/a><\/li>\n<li>Then create a private post or page or of any post type<\/li>\n<li>For custom post type template ensures that your template name looks like: <code>tpl-{{post_type}}<\/code><\/li>\n<li>or add a custom field to your post name it <strong>twig<\/strong>\u00a0and store the template name inside it<\/li>\n<\/ul>\n\n<h4>Custom Fields and ACF<\/h4>\n\n<ul>\n<li>WordPress supports custom fields, and you could add them directly to your post,<\/li>\n<li>and ACF plugin gives more power to the custom fields<\/li>\n<li>Use the Advanced Custom Fields plugin to take full control of your WordPress edit screens &amp; custom field data.<\/li>\n<li>Read more about ACF on <a href=\"https:\/\/wordpress.org\/plugins\/advanced-custom-fields\/\">https:\/\/wordpress.org\/plugins\/advanced-custom-fields\/<\/a><\/li>\n<\/ul>\n\n<h4>Template Syntax: a quick guide for WordPress Front End for Custom Field<\/h4>\n\n<p>Note: <a href=\"https:\/\/wordpress.org\/plugins\/classic-editor\/\">WordPress Classic Editor<\/a> is preferred to edit templates<\/p>\n\n<h4>Comments<\/h4>\n\n<ul>\n<li>Use <code>{# comments #}<\/code> for comments, and comments will not render to the user,<\/li>\n<\/ul>\n\n<h4>Variables output<\/h4>\n\n<p>Variables in templets are post title, post object, and custom fields. To output post title you can use <code>{{ title }}<\/code>. If your post has a custom field called price. We use <code>{{ price }}<\/code> to output the price. Use a dot (.) to access attributes of a variable (methods or properties of an object, or items of an array: <code>{{ foo.bar }}<\/code><\/p>\n\n<h4>Conditional output<\/h4>\n\n<p>The\u00a0<code>if<\/code> statement in Twig check if a variable has a value or an expression evaluates to\u00a0<code>true<\/code> We can add conditions to display any text. Let's say that we have a custom field named \"agent\" and want to display a text when the field agent contains a text. We can write<\/p>\n\n<pre>{% if agent %}\n    We have an agent in your area\n    Our agent: {{ agent }}\n{% endif %}<\/pre>\n\n<p>another example; let's say that we have a field named price. We want to display a text if the price is zero<\/p>\n\n<pre>{% if price == 0 %}\n   &lt;p&gt;This product is free&lt;\/p&gt;\n{% endif %}\n<\/pre>\n\n<p>Please note that we use the operator <code>==<\/code> to check the equality You can also test if an array is not empty:<\/p>\n\n<pre>{% if meanings %}\n   &lt;p&gt;The array or repeater field **meaning** contains some values&lt;\/p&gt;\n{% endif %}<\/pre>\n\n<h4>Closing condition block<\/h4>\n\n<p>Always use <code>{% endif %}<\/code> to close the previous if condition, Read more about <a href=\"https:\/\/twig.symfony.com\/doc\/3.x\/tags\/if.html\">if keyword in twig<\/a><\/p>\n\n<h4>Repeater field and arrays<\/h4>\n\n<p>If our field or variable is an array or a repeater field we can loop over each item in a sequence using for keyword<\/p>\n\nThe for\u00a0statement\u00a0code example\n\n<pre>{% for m in meanings %}\n   Meaning: {{ m.meaning }}\n{% endfor %}<\/pre>\n\nThe for statement format\n\n<ul>\n<li>We use\u00a0 <code>{% for m in meanings %}<\/code> to loop over an array or repeater field called <code>meanings<\/code> using <strong><code>m<\/code><\/strong> to mention for each row<\/li>\n<li>Inside the loop starts the subfields or array items with the variable name <code>**m.**<\/code><\/li>\n<li>Always close the loop use <code>{% endfor %}<\/code><\/li>\n<li>Read more about <a href=\"https:\/\/twig.symfony.com\/doc\/3.x\/tags\/for.html\">the <code>for<\/code> keyword in twig<\/a><\/li>\n<\/ul>\n\n<h4>Important notes for templates code<\/h4>\n\n<ul>\n<li>In WordPress classic editor show source and ensure there are no tags inside the template code; ex <code>{% &lt;del&gt;&lt;span&gt;&lt;\/del&gt; endfor &lt;del&gt;&lt;\/span&gt;&lt;\/del&gt; %}<\/code><\/li>\n<li>For more details, see: <a href=\"https:\/\/twig.symfony.com\/doc\/2.x\/\">https:\/\/twig.symfony.com\/doc\/2.x\/<\/a><\/li>\n<\/ul>\n\n<h4>Power templating with only 4 keyword<\/h4>\n\n<p>Using <code>if, endif, for and endfor<\/code> allows to generate a powerful template; however, twig contains many other useful keywords to see other keyword and how to use them visit <a href=\"https:\/\/twig.symfony.com\/doc\/2.x\/\">twig documentation<\/a><\/p>\n\n<h4>Short template example<\/h4>\n\n<pre>&lt;h2&gt;Meaning of {{ title }}&lt;\/h2&gt;\n{# This is a comment and will not render #}\n{% for m in meanings %}\n    {% if m.meaning %}\n        Meaning: {{ row.meaning }}\n    {% endif %}\n    {% if m.example %}\n        Example: {{ m.example }}\n    {% endif %}\n{% endfor %}\n<\/pre>\n\n<h4>Output post data<\/h4>\n\n<p>We can access the post using the post keyword. to access any property of the post we put a dot (.)\u00a0 and then we put the property name for example use <code>post.content<\/code> to access post content<\/p>\n\n<pre>{{ post.title }}\n   \n{{ post.content }}\n<\/pre>\n\n<p>read more about accessing post content on <a href=\"https:\/\/timber.github.io\/docs\/v2\">timber documents<\/a><\/p>\n\n<h4>Why templating engine and not a raw PHP programming language<\/h4>\n\n<ul>\n<li>More Simple; so templating is more readable, and very easy that allows users to learn quickly,<\/li>\n<li>and Limit the allowed function to prevent bad mistakes,<\/li>\n<li>and easy to edit using WordPress classic editor<\/li>\n<\/ul>\n\n<h4>About Twig templating engine<\/h4>\n\n<ul>\n<li>The twig template engine process templates data, and output a webpage.<\/li>\n<li>and it is a flexible, fast, and secure template engine for PHP.<\/li>\n<li>We use Twig a template language, and we allow users to modify the template design.<\/li>\n<li>It is fast because Twig compiles templates into optimized PHP code.<\/li>\n<li>Get flexible, so it allows the developer to define its own custom tags and filters.<\/li>\n<li>Read more about <a href=\"https:\/\/twig.symfony.com\">Twig<\/a><\/li>\n<\/ul>\n\n<h4>Timber and Twig<\/h4>\n\n<ul>\n<li>Timber helps to include Twig in WordPress,<\/li>\n<li>with Timber, you write your HTML using the Twig Template Engine separate from your PHP files,<\/li>\n<li>and this cleans up your template code so, for example, and your Twig template can focus on the HTML and display.<\/li>\n<\/ul>\n\n<h4>License and use<\/h4>\n\n<p>This plugin is distributed in the hope that it will be useful, but without any warranty. See the GNU General Public License for more details. http:\/\/www.gnu.org\/licenses\/.<\/p>\n\n<h3>For Developers<\/h3>\n\n<h4>Add custom data to fields<\/h4>\n\n<ul>\n<li>Edit the file user-functions.php<\/li>\n<li>Create a function and name it <code>get_fields_{{post_type}}<\/code><\/li>\n<li>If your custom post type contains \u2018-\u2018 replace it with \u2018_\u2019<\/li>\n<li>I include a sample function in user-functions.php<\/li>\n<li>Backup your functions, and keep in mind that the file user-function may be replaced on upgrade<\/li>\n<\/ul>\n\n<h4>Add custom data for each singular page<\/h4>\n\n<ul>\n<li>Edit the file user-functions.php<\/li>\n<li>then create a function and name it np_content_singular<\/li>\n<li>I include a sample function in user-functions.php<\/li>\n<li>Backup your functions, and keep in mind that the file user-function may be replaced on upgrade<\/li>\n<\/ul>\n\n<!--section=installation-->\n<ol>\n<li>Install <a href=\"https:\/\/wordpress.org\/plugins\/timber-library\/\">Timber plugin<\/a><\/li>\n<li>Upload this plugin folder to the <code>\/wp-content\/plugins\/<\/code> directory, or install it through the WordPress plugins screen directly.<\/li>\n<li>Activate the plugin through the \u2018Plugins\u2019 screen in WordPress<\/li>\n<li>and you will more benefit from our plugin if you could install \n<a href=\"https:\/\/wordpress.org\/plugins\/advanced-custom-fields\/\">Advanced Custom Fields<\/a><\/li>\n<\/ol>\n\n<!--section=faq-->\n<p>This section may be added in the next version<\/p>\n\n<!--section=changelog-->\n<h4>1.0<\/h4>\n\n<ul>\n<li>Plugin initial release<\/li>\n<\/ul>","raw_excerpt":"NP Twig Front End for WordPress Custom Field and Advance Custom Fields. Renders data using a twig template. It supports both free ACF and ACF pro.","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin\/139022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin"}],"about":[{"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wp\/v2\/types\/plugin"}],"replies":[{"embeddable":true,"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wp\/v2\/comments?post=139022"}],"author":[{"embeddable":true,"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wporg\/v1\/users\/miniindustry"}],"wp:attachment":[{"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wp\/v2\/media?parent=139022"}],"wp:term":[{"taxonomy":"plugin_section","embeddable":true,"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_section?post=139022"},{"taxonomy":"plugin_tags","embeddable":true,"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_tags?post=139022"},{"taxonomy":"plugin_category","embeddable":true,"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_category?post=139022"},{"taxonomy":"plugin_contributors","embeddable":true,"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_contributors?post=139022"},{"taxonomy":"plugin_business_model","embeddable":true,"href":"https:\/\/scn.wordpress.org\/plugins\/wp-json\/wp\/v2\/plugin_business_model?post=139022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}