npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@penskemediacorp/twig-to-php-parser

v1.62.0

Published

> TODO: description

Downloads

1,798

Readme

Twig To PHP Parser

This package is intended to work with Larva Patterns to both parse patterns into a parent theme and a child theme. It expects the following directory structure:

|- pmc-consuming-theme/
	|- assets/
		|- build/
		|- src/
			|- patterns/
				|- 04-components/
				|- 05-objects/
				|- 06-algorithms/
		|- larva.config.js
	|- template-parts/
		|- patterns/

Very Important To Know

~When parsing includes, the parser looks for the @larva namespace, and will point includes to PMC_CORE_PATH when it finds it. Otherwise, includes will point to CHILD_THEME_PATH.~ This is not in effect yet. Everything is contained to the child theme.

The parser supports a very limited amount of Twig and relies on keywords in data names in order to parse escaping correctly. Please refer to the Confluence documentation for Twig authoring guidelines: https://confluence.pmcdev.io/x/JoGJAg

Usage

First, add an entry for the parser to larva.config.js:

module.exports = {
	// Other config

	parser: {
	}
}

The parser object supports the following configuration:

	parser: {
		twigDir: path.resolve( './new-path/to-twig/' ), // Default /theme/assets/src/patterns.
		phpDir: path.resolve( './new-path/to-php/' ), // Default /theme/template-parts/patterns.
		isUsingPlugin: true, // Default false.
	}

Then, run the script from the assets directory by executing the package binary like so:

./node_modules/.bin/twig-to-php-parser

Supported wp action in twig markup:

{{ wp_action( 'action_name', arg1, arg2, ... ) }}

would translate to

<?php do_action( 'action_name', $arg1, $arg2, ... ); ?>

How To

TODO: Standardize and improve code structure for future improvement

Add a new function

  • New function add must follow twig function syntax: function_name( p1, p2, ... )

  • The new function_name must be added to larva/lib/server.js to support twig linting

      // Add custom function support for doing wp action: {{ function_name( ... ) }}
      twing.addFunction(new TwingFunction('function_name',() => {
      	return Promise.resolve(''); // must return a promise result
      }));
  • Add a new code block function/class to translate twig markup into php code block to twig-to-php-parse.php

    • Code block function should take a twig markup string as input

    • Return the translated php code block

        function parse_my_new_function( $twig_markup ) {
        	// Code to parse and translate $twig_markup
        	return $twig_patterns;
        }
        
    • Modify function twig_to_php_parser and call to the new parser function to process $twig_markup_replace_main variable

        $twig_markup_replace_main = parse_my_new_function( $twig_markup_replace_main ) 
  • Add unit test

    • Add js function to index.js to load the php code
    • Add unit test to test the new parser function