Skip to content

Template setValue #907

@ngocketit

Description

@ngocketit

Hi,

I'm new to PhpWord and am using this awesome library for one project. I need to make a template file and replace some info with data from user input. I use TemplateProcessor and the code looks something like so:

templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($template_file); $templateProcessor->setValue('firstname', $data['firstname']); $templateProcessor->setValue('lastname', $data['lastname']); $templateProcessor->saveAs('/tmp/filled.docx');
This, however, doesn't work as I expected. The Word file is a very simple file with a couple of lines inform of Key: Value. For example:

First name: firstname
Last name: lastname

The keys need to be replaced with real data from user input. There are a couple of things that I need your help:

  1. What should be the first parameter in setValue(). I suppose it's a word variable, right or can it be a piece of text?
  2. I'm using Word for Mac, how can I add a variable that can be used in setValue()? I can't seem to find any ways of doing that. On Mac, it has only things like AutoText, Field. I tried them all but none works.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions