-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Templating tables #1086
base: master
Are you sure you want to change the base?
Templating tables #1086
Conversation
- utility functions to parse lengths given in absolute CSS units (in. px, mm, cm, pc)
- insertion of a new table at a placeholder - deletion of a table containing a placeholder - deletion of a table row containing a placeholder
I like the findXmlBlockStart() functions, and that means changing a few bits in my code to accommodate for them. I do not like the naming convention for
Also in codestyle: Can you describe me the logic why inserting tables is needed beyond, say, cloneRow() ? CloneRow keeps the table formatting, so you can have nice tables. I did not see any formatting code in your functions, so you get a table, but just the basic one. You can just open a document with lots of blocks with tables with different sizes, then just Do not feel bad if this comes out harshly. (not meant to get you down, only to make the final code better). If I did not care about your code getting into 0.14.0 I would not even post. Can people reading this tell if they need templates where the amount of rows is not known, and this would be the only solution? (I saw questions about cloneColumn() in the field, so I suppose it is needed) |
Regarding the logic of why inserting tables is needed - I actually wrote this a couple of years ago so the details are a little hazy now, but I think we had a single placeholder that should be replaced either with a table or with a "No data available" type line, depending on conditions. The alternative of having two placeholders and deleting the unused one was unattractive as (i) we weren't the ones editing the templates so we'd have had to explain it and (ii) it was liable to end up with extra stuff in the template that wouldn't have got removed appropriately. You're right that the insertTable-functionality is a little basic, but maybe someone else can extend it with more formatting possibilities. As for the rest of your comments / code review: Constructive criticism is good! I'm happy to do some reworking if it helps get this into the next release. I'll look at the details when I get a bit more time - I assume there's no great rush, or is a 0.14.0 imminent? |
@ejn: No great rush, I assume old dev's will come back during the holiday season to do some work. We do have @troosan with access to the dev branch, but I want to make it very light for him (as he, like us?, does not want to be a maintainer). And I want to merge all new functionality and fixes into one clear way of doing things.
We see that the 0.13.0 branch has a bad regexp that does not work anymore (here listed is a working one.. but it's a bit messy)
See for example the Although that commit also is not touching the Block commands, leaving the faulty regexp in place, it does fix the inline block problem, I'll explain: This is a proper block. Each block has it's own paragraph
This is an inline block: And so, if the And then we have the
Then use
And get the same result with cloneBlock("bullet/",n) and ps: or exactly what you wanted it for: How do you feel about using I said something that was not true: The tables you inject can later be transformed with xslt, for example making more on tables: http:https://officeopenxml.com/WPtableGrid.php Once these search functions are created (find-given-tag-to-the left/right ), we make the rest of the functions use these functions. Right. I'm making a list of your functions (from the create Image branch) into my part (and add insert image from string), then merge your table stuff as I envision it (a little bit more lightweight than it currently is), then try to make them use the same base functions for less filesize, and propose that as a functional design (so function names exposed to the outside are 'logical' or at least, similar). Then you can shoot at it (and other people wanting to give their opinion, you are most welcome, as you are end-users of this) |
@ejn Ok here is a working copy of the TemplateProcessor.php. As you can see, I removed all your tag finding functions and replaced them with what I was using. This makes the amount of code much smaller. I also added a little to your testcases, and I will be incorporating your pastebin Tell me what you think. |
Ok, addressing the fact that we could do it in another way, let's leverage the Writer and IOFactory to make a colored table. This is pure 0.13.0
Do you see the potential for this v/s your tables? Can't you make a new version of your tables and:
that returns a subclass of PhpWord() with an extra function done() which will extract the table and replace it in the placeholder? No need for extra const TABLE_TEMPLATE, or the like. |
@ejn ... and you can use the same with addImage(), so IMAGE_TEMPLATE is not needed anymore (but you still retain the file injection code into the zip). edit: in fact, adding these to an internal array would keep them open until it is time to save/as or force them with a Which method do you think is better?
Where
where
The special case for setValue(), where it behaves like setBlock that cuts at w:p, but now cutting at w:r things to check: RAM considerations |
@FBnil Sorry for leaving you to talk to yourself for so long! I'll try and respond to all your main points. I love the idea of generating the blocks using the "main" PHPWord stuff and then just putting them in as XML: Certainly a much cleaner and more consistent strategy than having a bunch of pre-prepared sprintf templates. Equally XPath would be the "clean" way to do the template manipulation - but on the other hand I reckon you're right that that would be slower and more memory-intensive, and possibly less flexible than the regexes. Personally I like the hybrid approach to the API from your last post - for those that want full control of whatever blocks they're putting in they can generate them and then insert them, but keep the current simple API (single method call) for basic use-cases. For the image stuff where I wanted to allow the size of the image to be controlled from the template directly then this would not be possible with the "create object and then insert" approach as first the additional parameters must be read from the placeholder. (This functionality was to allow the template maintainer more flexibility without requiring that the code be changed). Oh, and yes to more uniform method names (setValue, setBlock, setImage, setTable, etc., even where many of these defer as much of the XML manipulation as possible to a common method such as setBlock). I also thought that it may be cleaner to put some of the Zip plumbing for adding additional files into a separate helper class, as this is secondary to the template processing but adds quite a lot of additional lines of code which clutter up the TemplateProcessor In terms of memory usage I think it would be better to transform the objects to XML immediately upon adding to the template rather than first recording them in an array; (i) this would probably need less memory and (ii) more importantly it allows e.g. adding a table with placeholders for images and then inserting images into them. I can't see a big advantage in deferring the serialization, or what is your idea behind this? The conversion of the column widths to Twips is possibly not required - I suspect I probably got this from studying what Word does (the whole images and tables was done by a combination of reading the theory and just deconstructing some docx files): It's quite possible that any valid units will work. Having had a look at your Pastebin it looks pretty good to me for reducing the amount of code. If you want to run with your ideas of generating PHPWord-objects for the blocks then I can try and support you (even if just by testing - unfortunately I've not got much time just now to work on this) and once there's a "new, improved" version I'll happily close my pull requests with reference to the better version. |
I LOVE the twipcode and want that in the product. (now that I tried it, it really is something I would miss, even if I do not think in inches). Images is a juggernautic task that you tackled well. I can think and play with tables and come up with something simple but effective (in a week or so, really need some days worth of sleep). |
So why it not merged? |
@troosan Do you have any information (about merge this pull request)? |
I'll try to merge this in next release. It needs a bit of work because lots of things will need merging |
2d9f999
to
e458249
Compare
Additional functions for working with tables in TemplateProcessor
(note split into two commits due to common changes in 02508f2 with Shared/Converter with #1084)