patternlab-php-core icon indicating copy to clipboard operation
patternlab-php-core copied to clipboard

Propose: {{XXX}} and ref->XXX in any data files

Open asbeseda opened this issue 8 years ago • 2 comments

At first, thanks for good framework, it very useful for me...

Propose to add a support of patterns in data.yaml and references to other data like this In data.yaml:

testA: 
  testB: BBBBB
testABByPattern: "{{testA.testB}}" #don't forgot "
testAByReference: ref->testA

I already realise this for my project. It useful for me in many cases :). Here is a code: at pattern-lab\core\src\PatternLab\PatternData\Helpers\PatternCodeHelper.php add at run() function

$data = Data::getPatternSpecificData($patternStoreKey);
	
/* ADDITION by [email protected] */
$this->myAdditionalProcessData($data, $patternLoader);

And new functions defenitions.....

/* ADDITION-BEGIN by [email protected] */
private function myAdditionalProcessDataRecursive($path, &$dataCurrentLevel, &$dataAll, $patternLoader, &$dataByPath){
	if(is_array($dataCurrentLevel)){
		foreach ($dataCurrentLevel as $nextKey => $nextValue) {
			$nextPath = $path . ((empty($path))?"":".") . $nextKey;
			$nextValue = $this->myAdditionalProcessDataRecursive($nextPath, $nextValue, $dataAll, $patternLoader, $dataByPath);
			$dataCurrentLevel[$nextKey] = $nextValue; 
		}
	} else if(is_string($dataCurrentLevel)) {
		if(strpos($dataCurrentLevel, '{{') !== false)
			$dataCurrentLevel = $patternLoader->render(array("pattern" => $dataCurrentLevel, "data" => $dataAll));
		if(strpos($dataCurrentLevel, 'ref->') !== false){
			$dataCurrentLevel = $dataByPath[str_replace("ref->","",$dataCurrentLevel)];
		}
	}
	if(!empty($path))
		$dataByPath[$path] = $dataCurrentLevel;
	return $dataCurrentLevel;
}
private function myAdditionalProcessData(&$patternData, $patternLoader){
	$dataByPath = Array();
	$this->myAdditionalProcessDataRecursive("", $patternData, $patternData, $patternLoader, $dataByPath);
}
/* ADDITION-END by [email protected]  */

asbeseda avatar Jan 31 '17 19:01 asbeseda

Code don't optimized, but worked. U can optimize it as u wish.

asbeseda avatar Jan 31 '17 19:01 asbeseda

Example from real life in my data.yaml

assets: "../../assets"
head-resources: |
  <link rel="stylesheet" href="{{assets}}/css/reset.css" type="text/css" />
  <link rel="stylesheet" href="{{assets}}/css/style.css" type="text/css" />
  <link rel="stylesheet" href="{{assets}}/css/grid12.css" type="text/css" />

asbeseda avatar Jan 31 '17 19:01 asbeseda