Я большой поклонник макросов C, но я подумал, что было бы неплохо, если бы вы могли писать макросы или «метакод» на том же языке, который вы используете. (C плохой пример; это может быть хорошо в скриптовых языках.)
В этих примерах я использую фигурные скобки для идентификации метакода. Вы сможете запустить исходный код через «препроцессор» для расширения метакода. В противном случае он будет расширен только один раз во время выполнения.
print "Your product ID is: ", { print '"', generateGUID(), '"' }
или
lookupTable[] = {
/* insert code to generate table here
*
* This lets you modify the algorithm easily
* but speeds up the final program.
*
* This would be especially appropriate in
* cases where you would otherwise type out
* the table as a literal (yuck)
*/
}
Время от времени мы должны написать несколько строк очень повторяющегося кода; сейчас не могу придумать хорошего примера, но такого рода мысли также были бы очень полезны в таких ситуациях.
<?php
//getting new instance
$pdfFile = new_pdf();
PDF_open_file($pdfFile, " ");
//document info
pdf_set_info($pdfFile, "Auther", "Ahmed Elbshry");
pdf_set_info($pdfFile, "Creator", "Ahmed Elbshry");
pdf_set_info($pdfFile, "Title", "PDFlib");
pdf_set_info($pdfFile, "Subject", "Using PDFlib");
//starting our page and define the width and highet of the document
pdf_begin_page($pdfFile, 595, 842);
//check if Arial font is found, or exit
if($font = PDF_findfont($pdfFile, "Arial", "winansi", 1)) {
PDF_setfont($pdfFile, $font, 12);
} else {
echo ("Font Not Found!");
PDF_end_page($pdfFile);
PDF_close($pdfFile);
PDF_delete($pdfFile);
exit();
}
//start writing from the point 50,780
PDF_show_xy($pdfFile, "This Text In Arial Font", 50, 780);
PDF_end_page($pdfFile);
PDF_close($pdfFile);
//store the pdf document in $pdf
$pdf = PDF_get_buffer($pdfFile);
//get the len to tell the browser about it
$pdflen = strlen($pdfFile);
//telling the browser about the pdf document
header("Content-type: application/pdf");
header("Content-length: $pdflen");
header("Content-Disposition: inline; filename=phpMade.pdf");
//output the document
print($pdf);
//delete the object
PDF_delete($pdfFile);
?>