Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A function returning all variables in docx template #2385

Open
nmauludina opened this issue Mar 9, 2023 · 1 comment
Open

A function returning all variables in docx template #2385

nmauludina opened this issue Mar 9, 2023 · 1 comment

Comments

@nmauludina
Copy link

Is your feature request related to a problem? Please describe.

I want to make dynamic form input base on all the variables exist in docx template.

Describe the solution you'd like

I think it's better if there is a function that can return all variables inside the docx

Additional context

If it feature already exist feel free to tell me

@cavasinf
Copy link

cavasinf commented Jun 27, 2023

For all variables -> getVariables():

/**
* Returns array of all variables in template.
*
* @return string[]
*/
public function getVariables()
{
return array_keys($this->getVariableCount());
}

If you want without blocks:

public function getVariablesWithoutBlock(): array
{
    $variables    = $templateProcessor->getVariables();
    
    $keysToRemove = [];
    $key_endBlock = null;
    foreach ($variables as $index => $variable) {
        if ($key_endBlock && $index <= $key_endBlock) {
            $keysToRemove[$index] = $index;
            continue;
        }

        $key_endBlock = array_search("/$variable", $variables, true)
        if ($key_endBlock) {
            // Is a block
            $keysToRemove[$index] = $index;
        }
    }

    return array_values(array_diff_key($variables, $keysToRemove));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants