So would like to share the small POC with code. Code will take Visualforce Page name as parameter and will return you the code written for that VFP as text. For example, this is my Visualforce Page saved with name "VisualforceCode"
<apex:page id="PG" controller="VisualforceCodeController">
<apex:form id="FRM">
<apex:pageMessages id="PM"/>
<apex:pageBlock id="PB">
<apex:commandLink value="See Magic" action="{!DisplayCode}" >
<apex:param assignTo="{!CurrentPageName}" value="{!$CurrentPage.Name}"/>
</apex:commandLink>
<br/> <br/>
<apex:outputPanel id="OPT" rendered="{!showCodeSection}">
<apex:outputLabel value="{!PageText}"/>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Now I want to read the code written in this page, so here is the working code which send page name as parameter in "UtilClass" to get the code as text:
public class VisualforceCodeController
{
//To display section which hold code as text of VF page
public boolean showCodeSection {get; set;}
//To hold VF Code as text
public String PageText {get; set;}
//Hold current page name
public String CurrentPageName {get; set;}
public VisualforceCodeController()
{
CurrentPageName = '' ;
showCodeSection = false ;
}
public PageReference DisplayCode()
{
if(CurrentPageName != '')
{
//Fetching VF code
PageText = UtilClass.VFPageCode(CurrentPageName) ;
showCodeSection = true ;
}
return null ;
}
}
To get the visualforce page name I've used "{!$CurrentPage.Name}" which is a global variable
Here Util Class code:
//General Class with static methods
public class UtilClass
{
//Method will take visualforce page name as parameter and returns the code written in it
public static String VFPageCode(String PageName)
{
ApexPage testPage = new ApexPage();
String PageText = '' ;
if(PageName != '')
{
//Fetching visualforce page code
testPage = [Select Id, Markup from ApexPage where name =: PageName];
PageText = testPage.markup ;
}
return PageText ;
}
}
There are many other methods you can use with "ApexPage" mentioned here. So if you want to update the VFP code from apex dynamically you can use "ApexPage"